Spring Data JPA无法找到我的自定义实现方法。

3

我正在尝试使用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)

在这里的文档中没有提到注释:http://docs.spring.io/spring-data/jpa/docs/1.5.3.RELEASE/reference/html/repositories.html#repositories.custom-implementations - Pracede
4
PersonRepositoryImpl 应该是一个类,而不是一个接口。 - Ricardo Veguilla
你正在使用哪个版本的spring-data-jpa? - Ricardo Veguilla
@Ricardo Veguilla,spring-data-jpa的版本是1.5。 - Pracede
@user2824691 我不需要findByName。 - Pracede
显示剩余2条评论
1个回答

9

我找到了答案。Spring-data-jpa使用一种惯例来编写CustomRepository。要完成此任务:我们必须像在Spring Data JPA文档中所解释的那样。我们必须创建一个接口,在其中添加自定义方法:

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

希望这能有所帮助。


谢谢,我认为他们应该概述实现方式,使用存储库本身的名称而不是自定义接口。 - David
谢谢你指出文档中具体的部分 - 这对我解决问题很有帮助(我的问题是有一个自定义存储库要在多个代码库之间共享)。 - dermoritz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接