3

I'm making a Spring project, where i'm using QueryDsl for the entities. I'm picking up this project from a few months back, where i already had 1 generated class (QUser). Now i made a new entity called Permission, and modified the User entity. When i'm building the project, the QUser doesn't change, and the QPermission class is not generating either. What am i doing wrong? Here's the entity and pom.xml for QueryDsl.

@Entity
@Table(name = "permission")
public class Permission {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;

   @Column(name = "name", length = 100, nullable = false)
   private String name;


   public Long getId() {
       return id;
   }

   public void setId(Long id) {
       this.id = id;
  }

    public String getName() {
       return name;
   }
    public void setName(String name) {
        this.name = name;
    }
}

And the pom.xml:

[..]
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <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>
        </plugin>
    </plugins>
</build>
[...]

I was following the documentation: http://www.querydsl.com/static/querydsl/3.2.0/reference/html/ch03s03.html I'm using IntellIJ IDEA and tried the "Rebuild project" option as well.

5 Answers 5

3

I was having trouble getting the files generated(the Q classes), and it turned out that building my maven project using IntelliJ would not work, so I had to run this command instead in the command line to build the project:

./mvnw install

And the files were generated for me, and in order to be able to use these generated files in my source code, I had to mark the folder that contains these files as "Generated Sources Root", so in this case navigate to this path:

target/generated-sources/java

right mouse click on java folder --> mark directory as --> Generated Sources Root.

After this you will be able to import these Q classes in your source code.

Sign up to request clarification or add additional context in comments.

1 Comment

I could not execute mvnw. But i ran mvn install. I checked my generated sources folder and the Q files were there. Then the second part of this answer helped me resolve the errors on Intellij
2

Can you show the dependencies you put in your pom.xml ?

I've made some tests starting from scratch

Here are the dependencies :

<dependencies>
    ...
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
        <version>4.2.1</version>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>4.2.1</version>
    </dependency>
    ...
</dependencies>

And the plugin :

        <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.1.3</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>

The complete source code I've tried to generate QFiles and that worked :

https://github.com/githubjul/test-so-querydsl

I don't run the project, only compile it to verify it works.

Comments

1

Here is a working pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.hello</groupId>
    <artifactId>querydsl</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>querydsl</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <querydsl.version>4.1.0</querydsl.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>${querydsl.version}</version>
            <scope>provided</scope>
        </dependency>

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

        <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>


        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <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</outputDirectory>
                            <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

and the generated class:

/**
 * QPermission is a Querydsl query type for Permission
 */
@Generated("com.querydsl.codegen.EntitySerializer")
public class QPermission extends EntityPathBase<Permission> {

    private static final long serialVersionUID = -479242270L;

    public static final QPermission permission = new QPermission("permission");

    public final NumberPath<Long> id = createNumber("id", Long.class);

    public final StringPath name = createString("name");

    public QPermission(String variable) {
        super(Permission.class, forVariable(variable));
    }

    public QPermission(Path<? extends Permission> path) {
        super(path.getType(), path.getMetadata());
    }

    public QPermission(PathMetadata metadata) {
        super(Permission.class, metadata);
    }

}

2 Comments

Could you explain to me, how you do it? Do i just change the pom.xml contents then just build? Or is there something else? I wanna make a lot more entities, and all of them are gonna need a Q class as well.
@iron24 Can you post your pom.xml? Also, how do you trigger the build, with: mvn clean package?
1

I was having this issue using IntelliJ.

The following steps worked for me:

  1. Right-clicked my POM,
  2. Clicked 'Maven',
  3. Clicked 'reload project'.

This seemed to do the trick.

Comments

0

Adding the following dependency did it for me:

<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
    <version>3.7.4</version>
</dependency>

You can look up the latest version here:

https://mvnrepository.com/artifact/com.mysema.querydsl/querydsl-jpa

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.