Java JPA Projection in Repository— No property found for type
May 6, 2024
The Problem
You have the JPA projection by interface like this
public interface PaymentSummaryReponse {
String getCode();
BigDecimal getAmount();
}
Then you got the problem when compile
No property 'YourMethod' found for type 'YourEntity'
You even try to change the method name to foo
to see what is the problem.
public interface PaymentSummaryRepository extends JpaRepository<PaymentSummary, Integer> {
List<PaymentSummaryResponse> foo();
}
Solution : Naming Convention
You need to name the method to be the same with JPA method
For example, findByFoo
or other convention
Because Spring Boot treat the projection as an ‘Add-on’ to these existing method.
In my case, findAll
will be findAllProjectedBy
public interface PaymentSummaryRepository extends JpaRepository<PaymentSummary, Integer> {
List<PaymentSummaryResponse> findAllProjectedBy();
}