Spring Data JPA Projection selected fields from the DB

后端 未结 2 1999
灰色年华
灰色年华 2020-12-23 23:16

I was testing Spring Data 1.10.4.RELEASE, following the example in Spring Data Docs http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections

an

相关标签:
2条回答
  • 2020-12-23 23:30

    I had the same issue and when I changed projections from interfaces to pojo classes it generated SQL properly.

    0 讨论(0)
  • 2020-12-23 23:44

    If you want use the annotation @Query with Spring Data Projections you have to use field alias and you need to make sure you alias the projects matching the projection fields. The following code should work for question 1:

    interface PersonRepository extends CrudRepository<Person, Long> {
    
      @Query("select p.firstName as firstname, p.address as address from Person p where p.firstName = ?1")
      PersonLimited findPersonByFirstNameProjectedForLimitedData(String firstName);
    }
    

    Another alternative that you can use is define your queries with Property Expressions. whenever is possible:

    interface PersonRepository extends CrudRepository<Person, Long> {
    
      List<PersonLimited> findByFirstName(String firstName);
    }
    
    0 讨论(0)
提交回复
热议问题