Spring数据jpa找不到我的自定义实现方法

时间:2014-08-13 14:46:23

标签: java spring spring-data-jpa

我正在尝试使用Spring Data JPA提供存储库的自定义实现。我有:

public interface PersonRepositoryCustom{

    List<Person> chercher(String name);

}

实施:

 public class PersonRepositoryImpl implements PersonRepositoryCustom{

        List<Person> chercher(String name){ 
// my implementation
         }

    }

这两个类在jpa:repositories包中

这是我的人员DAO:

public interface IPersonDAO extends CrudRepository<Person, Long>,PersonRepositoryCustom{ 
// other methods here
}

当我启动服务器时,我收到错误:

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)

1 个答案:

答案 0 :(得分:6)

我找到了答案。 Spring-data-jpa使用约定来编写CustomRepository。要完成这项任务:我们必须按照Spring Data JPA documentation中的说明进行操作。我们必须创建一个接口,我们在其中添加自定义方法:

public interface PersonRepositoryCustom{

    List<Person> chercher(String name);

}

假设我们有以下DAOService:

public interface IPersonDAO extends CrudRepository<Person, Long>,PersonRepositoryCustom{ 
// other methods here
}

因此,自定义存储库的实现是: IPersonDAOImpl

public class IPersonDAOImpl implements PersonRepositoryCustom{

        List<Person> chercher(String name){ 
// my implementation
         }

    }

而不是 PersonRepositoryImpl

我希望这会有所帮助。