简体   繁体   中英

QueryDsl classes are not generated in IntelliJ

I have the following class and I would like to try out querydsl and make some basic queries. Using intelliJ 2017.3, no class QUser is generated. I've tried googling my problem and each SO answer seems to offer a different solution (some didn't work and some I did not understand cause I've never used this things before) and most tutorials seem to do completely different stuff.

I have tried making queries using whatever Spring Boot seems to have built-in (no idea, simply seems to work, but it's too basic from the looks of it) and the queries work just fine so I'm guessing it's some configuration issue (I'm a maven and spring noob).

// User.java
@Entity
@Table(name = "symptoms")
public class Symptom
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    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;
    }
}

I have added these things to the pom.xml:

    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
        <version>4.1.4</version>
    </dependency>

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

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.25</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>
        </plugin>

You can fix the problem by: Open target folder --> Right click to generated-sources folder -> Mark Directory As -> Generated sources root .

After correctly marking the generated sources folder it worked without any problems for me. If IntelliJ doesn't instantly recognize the marked folder and the error still persists, mvn clean install usually helps.

I have managed to generate the Q classes using good ol' code. Just call this once and the classes will be generated in target/generated-sources/java . You can change this in the second-to-last line.

GenericExporter exporter = new GenericExporter();
exporter.setKeywords(Keywords.JPA);
exporter.setEntityAnnotation(Entity.class);
exporter.setEmbeddableAnnotation(Embeddable.class);
exporter.setEmbeddedAnnotation(Embedded.class);
exporter.setSupertypeAnnotation(MappedSuperclass.class);
exporter.setSkipAnnotation(Transient.class);
exporter.setTargetFolder(new File("target/generated-sources/java"));
exporter.export(ApplicationClass.class.getPackage());

In the last line, ApplicationClass is the class that starts the spring application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM