spring-data-jpa icon indicating copy to clipboard operation
spring-data-jpa copied to clipboard

Rewrite string-queries to use constructor expressions when return type is DTO

Open gregturn opened this issue 2 years ago • 0 comments

Consider the following query:

@Entity
class Person { … }

class PersonProjection { … }


@Query("SELECT p FROM Person p")
PersonProjection findBy(…)

The query selects items from the Person entity while its return type is a DTO projection.

It would be good to rewrite such queries to use DTO projections for matching properties along the lines of:

@Query("SELECT new com.acme.PersonProjection(p.firstName, p.lastName) FROM Person p")
PersonProjection findBy(…)

gregturn avatar Jul 19 '23 14:07 gregturn