我在com.querydsl中自动生成Q classess有问题。我知道com.query.dsl和spring-boot-jpa之间的兼容性可能存在错误,但我尝试了很多版本而没有任何正面结果。这是我的pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212.jre7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>4.0.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<id>process-common-model</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/apt</outputDirectory>
<processors>
<processor>com.query.apt.jpa.JPAAnnotationProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.0.7</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
错误
引起:org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.jpa.repository.support.QueryDslJpaRepository]:构造函数抛出异常;嵌套异常是java.lang.IllegalArgumentException:没有为域类com.coig.prek.webservice.modules.quiz.entiry.Quiz找到查询类com.coig.prek.webservice.modules.quiz.entiry.QQuiz! 在org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepositoryViaReflection(RepositoryFactorySupport.java:365)〜[spring-data-commons-1.13.0.RELEASE.jar:na] 在org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:101)〜[spring-data-jpa-1.11.0.RELEASE.jar:na] 在org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81)〜[spring-data-jpa-1.11.0.RELEASE.jar:na] 在org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199)〜[spring-data-commons-1.13.0.RELEASE.jar:na] 在org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277)〜[spring-data-commons-1.13.0.RELEASE.jar:na] 在org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263)〜[spring-data-commons-1.13.0.RELEASE.jar:na] 在org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101)〜[spring-data-jpa-1.11.0.RELEASE.jar:na] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] ...省略了47个常见帧 引起:java.lang.IllegalArgumentException:没有为域类com.coig.prek.webservice.modules.quiz.entiry.Quiz找到查询类com.coig.prek.webservice.modules.quiz.entiry.QQuiz! 在org.springframework.data.querydsl.SimpleEntityPathResolver.createPath(SimpleEntityPathResolver.java:63)〜[spring-data-commons-1.13.0.RELEASE.jar:na] 在org.springframework.data.jpa.repository.support.QueryDslJpaRepository。(QueryDslJpaRepository.java:84)〜[spring-data-jpa-1.11.0.RELEASE.jar:na] 在org.springframework.data.jpa.repository.support.QueryDslJpaRepository。(QueryDslJpaRepository.java:68)〜[spring-data-jpa-1.11.0.RELEASE.jar:na] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)〜[na:1.8.0_111] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)〜[na:1.8.0_111] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)〜[na:1.8.0_111] 在java.lang.reflect.Constructor.newInstance(Constructor.java:423)〜[na:1.8.0_111] 在org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)〜[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE] ...省略了56个常见帧 引起:java.lang.ClassNotFoundException:com.coig.prek.webservice.modules.quiz.entiry.QQuiz at java.net.URLClassLoader.findClass(URLClassLoader.java:381)〜[na:1.8.0_111] 在java.lang.ClassLoader.loadClass(ClassLoader.java:424)〜[na:1.8.0_111] at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331)〜[na:1.8.0_111] 在java.lang.ClassLoader.loadClass(ClassLoader.java:357)〜[na:1.8.0_111] 在org.springframework.util.ClassUtils.forName(ClassUtils.java:250)〜[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在org.springframework.data.querydsl.SimpleEntityPathResolver.createPath(SimpleEntityPathResolver.java:53)〜[spring-data-commons-1.13.0.RELEASE.jar:na] ...省略了63个常见帧
存储库:
@Repository
public interface QuizRepository extends PagingAndSortingRepository<Quiz, Long>, QueryDslPredicateExecutor<Quiz> {
}
和实体:
@javax.persistence.Entity
@Table(name = "QUIZ", uniqueConstraints = {
@UniqueConstraint(columnNames = "NAME")
})
@ToString
public class Quiz extends Entity{
@Column(name = "NAME")
private @Setter @Getter String name;
@Column(name = "TYPE")
private @Setter @Getter Integer type;
@Column(name = "TIME")
private @Setter @Getter Long time;
@JsonProperty(value = "branch")
@Column(name = "BRANCH")
private @Setter @Getter Integer branch;
@OneToMany
private @Setter @Getter Set<Question> questions;
public Quiz() {}
}
你能告诉我我是否遗漏了什么,或者我做错了什么? 谢谢你的帮助。
答案 0 :(得分:1)
以下是有关如何使用QueryDSL的示例:
在pom.xml
文件中包含以下依赖项:
<dependency>
<groupId>io.springlets</groupId>
<artifactId>springlets-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
此外,请包含以下插件以生成Q
类:
<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>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
</plugin>
现在,创建一个名为QuizRepositoryCustom
的新存储库接口,并定义要提供的方法。
public interface QuizRepositoryCustom {
// SAMPLE METHOD TO SEARCH QUIZ BY NAME
public Page<Quiz> findByNameLike(String name, Pageable pageable);
}
创建QuizRepositoryCustom
的实现。此类应扩展QueryDslRepositorySupport
public class QuizRepositoryImpl extends QueryDslRepositorySupport<Quiz> implements QuizRepositoryCustom {
QuizRepositoryImpl() {
super(Quiz.class);
}
public Page<Quiz> findByNameLike(String name, Pageable pageable) {
QQuiz quiz = QQuiz.quiz;
JPQLQuery<Quiz> query = from(quiz);
if (StringUtils.isNotEmpty(name)) {
BooleanBuilder searchCondition = new BooleanBuilder();
searchCondition.and(quiz.name.eq(name));
if (searchCondition.hasValue()) {
query.where(searchCondition);
}
}
long totalFound = query.fetchCount();
List<Quiz> results = query.select(quiz).fetch();
return new PageImpl<M>(results, pageable, totalFound);
}
}
现在,从QuizRepository
和JpaRepository<Quiz, Long>
界面扩展QuizRepositoryCustom
界面。
public interface QuizRepository extends QuizRepositoryCustom, JpaRepository<Quiz, Long> {
}
通过这些简单的步骤,您就可以使用QueryDSL。
此外,您可以查看以下项目,其中包含有关如何使用QueryDSL的更多示例。
希望它有所帮助,
答案 1 :(得分:0)
几天前,我也遇到了这个问题!!但这与您的问题有点不同,它类似于:
Failed to introspect Class org.springframework.data.jpa.repository.support.QuerydslJpaRepository] from ClassLoader [sun.misc.Launcher$AppClassLoader@764c12b6]; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.data.jpa.repository.support.QuerydslJpaRepository] from ClassLoader [sun.misc.Launcher$AppClassLoader@764c12b6]
但是我认为这值得尝试:
这是场景::我克隆了一个具有querydsl实现并正在使用STS IDE的项目。
在下载所有依赖项时,我运行了该项目并成功运行,但是在访问任何端点时,上述错误开始提示!!
这就是我要做的:
我进入.m2
文件夹,删除了querydsl-jpa
中的com/querydsl directory
文件夹,然后手动运行mvn clean [ install for war | package for jar ]
,下载了所有依赖项并正确链接了它们! / p>
重新运行spring项目时,一切工作正常。
此问题的原因:有时从maven或任何其他存储库下载依赖项时,jar文件可能会损坏,这在执行之前是无法检测到的,因此删除.m2文件夹中的jar是有益的并且有时会手动运行,因为IDE也会破坏下载的jar。
希望这可以解决您的问题!