自定义存储库基类 + QueryDslPredicateExecutor [英] Custom Repository Base Class + QueryDslPredicateExecutor
问题描述
我发现 QueryDslPredicateExecutor
对于减少样板文件非常有用,但它似乎是在投入工作.我现在正在尝试使用自定义基类存储库扩展 JpaRepository
,并且在启动时,Spring 无法正确实例化存储库.
I have found QueryDslPredicateExecutor
very useful for reducing boilerplate, but it seems to be throwing a monkey wrench into the works. I'm now trying to extend JpaRepository
with a custom base class repository, and on startup, Spring is having trouble instantiating repositories correctly.
//Custom base class
@NoRepositoryBean
public interface IdAwareRepository<A, ID extends Serializable> extends JpaRepository<A, ID> {
// ID getId(A a);
}
// Base class implementation
public class IdAwareRepositoryImpl<A, ID extends Serializable>
extends SimpleJpaRepository<A, ID> implements IdAwareRepository<A, ID> {
public IdAwareRepositoryImpl(JpaEntityInformation<A, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
}
}
// Individual repo
@Repository
public interface MyPojoRepository extends JpaRepository<MyPojo, Integer>, QueryDslPredicateExecutor<MyPojo> {
}
// Spring boot main application class
@EnableJpaRepositories(repositoryBaseClass = IdAwareRepositoryImpl.class)
@EntityScan(basePackageClasses = {Application.class, Jsr310JpaConverters.class})
@EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration.class})
@SpringBootApplication
public class Application {}
我已经尝试了关于这个主题的几种变体,但没有成功地将事情连接起来.我在 Spring 的问题跟踪器 https://jira.spring.io/browse/DATAJPA 上遇到了类似的问题-674,但没有对修复的解释,只是重构了代码以使其更易于使用.
I've tried several variations on this theme, but have not had luck in getting things wired up successfully. I came across a similar issue on Spring's issue tracker https://jira.spring.io/browse/DATAJPA-674, but no explanation on the fix, just that code was being refactored to be easier to work with.
我收到以下错误:
原因:org.springframework.data.mapping.PropertyReferenceException:否为 MyPojo 类型找到属性 findAll!在org.springframework.data.mapping.PropertyPath.(PropertyPath.java:77)在org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329)在org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309)在org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272)在org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243)在org.springframework.data.repository.query.parser.Part.(Part.java:76)在org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:235)在org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373)在org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:353)在org.springframework.data.repository.query.parser.PartTree.(PartTree.java:84)在org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:62)在org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:100)在org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:211)在org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:74)在org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:416)在org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:206)在org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)在org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)在org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type MyPojo! at org.springframework.data.mapping.PropertyPath.(PropertyPath.java:77) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) at org.springframework.data.repository.query.parser.Part.(Part.java:76) at org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:235) at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373) at org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:353) at org.springframework.data.repository.query.parser.PartTree.(PartTree.java:84) at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:62) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:100) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:211) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:74) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:416) at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:206) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237) at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
我认为 Spring 无法将自定义基类和 QueryDslPredicateExecutor
扩展连接到 JpaRepository
Which to me says that Spring isn't able to wire up both the custom base class and QueryDslPredicateExecutor
extensions to JpaRepository
推荐答案
我通过让我的基础存储库扩展 QueryDslMongoRepository 解决了一个类似的问题.
您或许可以扩展一个类似的类.
I solved a similar issue by having my base repository extend the QueryDslMongoRepository.
You may be able able to extend a similar class.
"不存在任何属性找到类型...当将 QueryDslPredicateExecutor 与 MongoDB 和 Spring-Data 一起使用时
这篇关于自定义存储库基类 + QueryDslPredicateExecutor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!