Pra*_*ede 1 java spring spring-data-jpa
我正在尝试使用Spring Data JPA提供存储库的自定义实现.我有 :
public interface PersonRepositoryCustom{
List<Person> chercher(String name);
}
Run Code Online (Sandbox Code Playgroud)
并实施:
public class PersonRepositoryImpl implements PersonRepositoryCustom{
List<Person> chercher(String name){
// my implementation
}
}
Run Code Online (Sandbox Code Playgroud)
这两个类在jpa:repositories包中
这是我的人DAO:
public interface IPersonDAO extends CrudRepository<Person, Long>,PersonRepositoryCustom{
// other methods here
}
Run Code Online (Sandbox Code Playgroud)
当我启动服务器时,我收到错误:
org.springframework.data.mapping.PropertyReferenceException: No property chercher found for type Person
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:201)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:291)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:271)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:80)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:91)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:304)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:161)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:84)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
Run Code Online (Sandbox Code Playgroud)
我找到了答案.Spring-data-jpa使用约定来编写CustomRepository.要完成这项任务:我们必须按照Spring Data JPA文档中的说明进行操作.我们必须创建一个接口,我们在其中添加自定义方法:
public interface PersonRepositoryCustom{
List<Person> chercher(String name);
}
Run Code Online (Sandbox Code Playgroud)
假设我们有以下DAOService:
public interface IPersonDAO extends CrudRepository<Person, Long>,PersonRepositoryCustom{
// other methods here
}
Run Code Online (Sandbox Code Playgroud)
所以自定义存储库的实现是:IPersonDAOImpl
public class IPersonDAOImpl implements PersonRepositoryCustom{
List<Person> chercher(String name){
// my implementation
}
}
Run Code Online (Sandbox Code Playgroud)
而不是PersonRepositoryImpl
我希望这将有所帮助.
| 归档时间: |
|
| 查看次数: |
1721 次 |
| 最近记录: |