Spring Data JPA Custom Repository

徘徊边缘 提交于 2019-12-14 04:04:51

问题


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

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

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

Entity:

public class MyEntity {

    private Integer id
    private String name;

    // Getter/Setters...
}

Service:

@Service
public class MyServiceImpl implements MyService {


    @Autowired
    private MyRepository repository;

    //...

    public MyDTO customFind(Long id){


        return repository.customFind(id);
    }
}

Repository:

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

    //no-op
}

Custom Repository:

public interface MyCustomRepository {

    List<MyDTO> customFind(Long id);

}

Custom Repository Impl:

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;

    }
}

When i run the application i got PropertyReferenceException:

Exception:

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)

回答1:


The type of entity and ID that it works with,MyEntity and Long, are specified in the generic parameters on JpaRepository. That is what your custom repository should be working with. So your MyCustomRepositoryImpl should use MyEntity instead of MyDTO.




回答2:


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

Now works fine! =)



来源:https://stackoverflow.com/questions/50032101/spring-data-jpa-custom-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!