我正在尝试在我的 eclipse maven 项目中使用 QueryDSL。这些是依赖项。
<properties>
<!-- The main class to start by executing java -jar -->
<start-class>my.app.market.DBApp</start-class>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<querydsl.version>4.1.4</querydsl.version>
<apt-maven-plugin.version>1.1.3</apt-maven-plugin.version>
</properties>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.1.4</version>
<scope>provided</scope>
</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.6.1</version>
</dependency>
<build>
<plugins>
<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>
在此之后,我尝试编写查询。
@Repository
public class QueryDSLRepo {
@PersistenceContext
private EntityManager em;
public ReportingParamDAO save(final ReportingParamDAO reportingParamDAO) {
em.persist(reportingParamDAO);
return reportingParamDAO;
}
public List<ReportingParamDAO> findreportingParamDAOsByIdQueryDSL(final Integer id) {
final JPAQuery<ReportingParamDAO> query = new JPAQuery<>(em);
final QReportingParamDAO reportingParamDAO = QReportingParamDAO.reportingParamDAO;
return query.from(reportingParamDAO).where(reportingParamDAO.id.eq(id)).fetch();
}
}
但是我得到了错误
QReportingParamDAO cannot be resolved to a type
注意:ReportingParamDAO
是一个实体类。
这意味着没有生成我的 DAO 的 Q 类型类。我不确定为什么没有生成它。我需要做其他事情吗?我遇到了this发布,但用户正在使用 IntelliJ
,我似乎无法让它在我的情况下工作。有人可以帮帮我吗。谢谢!!
最佳答案
我已经用你的 pom.xml 进行了测试。 Q 类是为我生成的,但我无法从我的源代码访问它们。问题是默认情况下生成的源不在类路径中。将其添加到类路径中,您将能够在源代码中使用它们。
- 检查 target/generated-sources 目录以查看这些类是否确实存在。 (你应该能够找到它们,因为我用你的 pom.xml 进行了测试)
- 如果您将目标/生成源添加到类路径,您的应用程序就会工作。但我认为这不是一个好主意。因为类路径中的所有文件都会被 IDE 索引,你的 IDE 会更慢。 generated-sources 文件夹中的所有文件都不需要被索引。因此,将
target/generated-sources/java
添加到类路径并将您的 query-dsl 插件更改为生成的 Q 类到target/generated-sources/java
关于java - 未生成查询 DSL Q 类型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45794079/