创建Spring JPA全局自定义仓库时遇PropertyReferenceException错误求助
问题描述
我已查看相关页面,但并非同一问题,Stack Overflow自动关闭机制误判了我的提问。我正尝试创建一个可为所有仓库添加自定义行为的Spring Data JPA自定义仓库,虽已遵循各类教程,但仍持续报错:
"PropertyReferenceException: No property 'advancedSearch' found for type 'X'"
相关信息
POM依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.3</version> <relativePath /> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
代码部分
JpqlQuery为自定义类,自定义仓库接口如下:
@NoRepositoryBean public interface QueryRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { QueryResults<T> advancedSearch(JpqlQuery<T> query) throws Exception; }
我创建了如下子接口继承该接口:
public interface AddressRepository extends QueryRepository<Address,Long> { }
实现类命名为接口名加"Impl",已尝试放在接口的子包和同包下,均无效果:
public class QueryRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements QueryRepository<T, ID> { @PersistenceContext private final EntityManager em; public QueryRepositoryImpl(Class<T> domainClass, EntityManager em) { super(domainClass, em); this.em = em; } public QueryRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager em) { super(entityInformation, em); this.em = em; } @Override @Transactional public QueryResults<T> advancedSearch( JpqlQuery<T> query) throws Exception { ... } }
配置信息
我已在主应用类的@ComponentScan中添加实现类包名:
@EnableEurekaClient @SpringBootApplication @ComponentScan({ // "com.mycompany.common.repositories.impl" // For QueryRepositoryImpl }) public class CoreRestServiceApplication { ... }
并在服务的配置类中添加:
@Configuration @EnableJpaRepositories(// basePackages = {"com.mycompany.common.repositories.impl"}, // repositoryBaseClass = QueryRepositoryImpl.class) // takes the place of the QueryRepositoryFactoryBean public class ApplicationConfiguration { ... }
疑问
QueryRepository和QueryRepositoryImpl在一个jar包中,而使用它们的服务在不同的jar/项目中,这会不会是问题所在?我哪里遗漏或操作错误了?
内容的提问来源于stack exchange,提问作者Gary Kephart
相关产品推荐
相关产品推荐

