Spring Data JPA 自定义存储库 [英] Spring Data JPA Custom Repository

查看:57
本文介绍了Spring Data JPA 自定义存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用 Spring Data JPA 实现自定义存储库时遇到了一些问题.

I'm having some issues when trying to implement Custom Repositorys with Spring Data JPA.

我尝试遵循一些这样的参考指南,但我找不到问题:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations

I try follow some references guides like this but I cant find the problem: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-implementations

我不知道为什么 Spring 试图查找自定义存储库方法作为我的实体属性.

I do not know why Spring tries to look up the custom repository method as my entity property.

spring-boot-starter-data-jpa => 1.5.11.RELEASE

实体:

public class MyEntity {

    private Integer id
    private String name;

    // Getter/Setters...
}

服务:

@Service
public class MyServiceImpl implements MyService {


    @Autowired
    private MyRepository repository;

    //...

    public MyDTO customFind(Long id){


        return repository.customFind(id);
    }
}

存储库:

@Repository
public interface MyRepository extends JpaRepository<MyEntity, Long>, QueryDslPredicateExecutor<MyEntity>, MyCustomRepository {

    //no-op
}

自定义存储库:

public interface MyCustomRepository {

    List<MyDTO> customFind(Long id);

}

自定义存储库实现:

public class MyCustomRepositoryImpl implements MyCustomRepository {


    @Autowired
    private EntityManager entityManager;


    public List<MyDTO> customFind(Long id){


        JPAQuery<EmpregadoEntity> query = new JPAQuery<>(entityManager);
        MyDTO myDTO = query...  //... JPA query return MyDTO

        return myDTO;

    }
}

当我运行应用程序时,我得到了 PropertyReferenceException:

When i run the application i got PropertyReferenceException:

异常:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property customFind found for type MyEntity!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:79)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:335)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:311)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:274)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245)
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:247)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:378)
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:86)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:70)

推荐答案

我只是将类MyCustomRepository"重命名为MyRepositoryCustom"来解决问题.

I just rename class "MyCustomRepository" to "MyRepositoryCustom" to fix the problem.

现在工作正常!=)

这篇关于Spring Data JPA 自定义存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆