1

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)
rafambbr
  • 570
  • 8
  • 15

2 Answers2

0

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.

Luay Abdulraheem
  • 751
  • 3
  • 11
0

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

Now works fine! =)

rafambbr
  • 570
  • 8
  • 15