1

Hi I have a question about code generation when combining QueryDSL JPA and SQL. According to the answer of a SO question of the creator of QueryDQL, the combination of these two modules is a popular usage for CRUD manipulation(JPA) and queries(SQL).

After succeeding useing only JPA module, I tried to add SQL module but I dont know whether or not I should add the code generation of SQL to pom.

In fact I'm looking for insert function and I cannot find it at JPA module, that's why I'm trying to use SQL.

Also, when I tried to extends Spring Repository from QueryDslPredicateExecutor, there is an error :

The type com.mysema.query.types.Predicate cannot be resolved. It is indirectly referenced from required .class files.

Someone said it's Eclipse configuration issue but I dont get to solve the problem.

QueryDSL version is 4.1.2 and Spring Boot is 1.3.5. So what is the right version to use?

pom.xml
    ....
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
        <version>${querydsl.version}</version>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>${querydsl.version}</version>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-sql-spring</artifactId>
        <version>${querydsl.version}</version>
    </dependency>
    ....
    <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.1.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/generated-sources/java</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.querydsl</groupId>
                    <artifactId>querydsl-apt</artifactId>
                    <version>${querydsl.version}</version>
                </dependency>
            </dependencies>
        </plugin>
mingzhao.pro
  • 562
  • 1
  • 4
  • 16
  • Spring Boot 1.3.5, has a spring-data-jpa version for querydsl 3 not 4. Also why would you need to generate inserts if you use JPA already? Basically beats the purpose of JPA altogether. – M. Deinum Jun 15 '16 at 07:07
  • For now it's just for learning QueryDSL and I find only insert function at SQL module. – mingzhao.pro Jun 15 '16 at 07:22
  • Any update on this? the querydsl-core jar clearly contains the Predicate class, I don't get why eclipse is complaining – chrismarx Jul 29 '16 at 14:37

3 Answers3

1

The comment from M. Deinum should be the accepted answer. Spring boot 1.3.5 does indeed depend on spring-data-jpa 1.9.4, which is setup to use querydsl 3.*, you can see that it references packages like this:

com.mysema.query.types.Predicate

and as of querydsl 4.0, the new packages are all:

com.querydsl.*

Looks like we'll need to upgrade to spring boot 1.4, which is based on spring-data 1.10, which uses querydsl 4.0

http://docs.spring.io/spring-data/jpa/docs/1.10.2.RELEASE/reference/html/#new-features

chrismarx
  • 8,268
  • 7
  • 69
  • 85
  • i have question, As Spring Boot 1.3.5 have QueryDSL added then do i need to make this as a part of maven dependency and in plugin as well? or i can skip the configuration and directly use QueryDslPredicateExecutor class. – Anchit Pancholi Aug 14 '16 at 00:29
  • Check out jhipster, after creating a basic project (https://jhipster.github.io/) checkout their tip for adding querydsl- https://jhipster.github.io/tips/003_tip_add_querydsl_support.html – chrismarx Aug 15 '16 at 14:47
0

Instead of using apt-maven-plugin you can just use annotation processor from querydsl-apt by adding jpa classifier:

<dependency>
  <groupId>com.querydsl</groupId>
  <artifactId>querydsl-apt</artifactId>
  <version>${querydsl.version}</version>
  <classifier>jpa</classifier>
</dependency>

This way both Maven and your IDE (IDEA, for example) can generate the Q classes and you don't need to specify apt-maven-plugin.

For Querydsl 4 you either need to use Spring Boot 1.4.0+ or override spring-data-commons (1.12.1.RELEASE) and spring-data-jpa (1.10.2.RELEASE) in dependencyManagement.

lpandzic
  • 4,914
  • 4
  • 35
  • 46
0

If you're using org.springframework.data.jpa.repository.JpaRepository, you can use:

User user = repository.save(user);

Here is from CrudReporsitory.save() javadoc:

Saves a given entity. Use the returned instance for further operations as the save operation might have changed the entity instance completely.

If the entity isn't new, merge is called. merge copies the state of its argument into the attached entity with the same ID, and returns the attached entity. If the entity isn't new, and you don't use the returned entity, you'll make modifications to a detached entity.

I banged my head for hours to make
SQLQueryFactory queryFactory = new SQLQueryFactory(configuration, dataSource); work,

and then I banged it for the last time and remembered that there's this save method.

Good answer about save here.

Community
  • 1
  • 1
Filip Savic
  • 1,828
  • 17
  • 23