1
votes

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>
3
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
For now it's just for learning QueryDSL and I find only insert function at SQL module. - mingzhao.pro
Any update on this? the querydsl-core jar clearly contains the Predicate class, I don't get why eclipse is complaining - chrismarx

3 Answers

1
votes

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

0
votes

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.

0
votes

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.