You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/antora/modules/ROOT/pages/repositories/projections.adoc
+35
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,41 @@ NOTE: It is important to note that <<projections.dtos,Class-based projections>>
10
10
And it's important to point out that class-based projections do not work with native queries AT ALL.
11
11
As a workaround you may use named queries with `ResultSetMapping` or the Hibernate-specific javadoc:{hibernatejavadocurl}org.hibernate.query.ResultListTransformer[]
12
12
13
+
== DTO Projection JPQL Query Rewriting
14
+
15
+
JPQL queries allow selection of the root object, individual properties, and DTO objects through constructor expressions.
16
+
Using a constructor expression can quickly add a lot of text to a query and make it difficult to read the actual query.
17
+
Spring Data JPA can support you with your JPQL queries by introducing constructor expressions for your convenience.
record UserDto(String firstname, String lastname){}
35
+
----
36
+
37
+
<1> Selection of the top-level entity.
38
+
This query gets rewritten to `SELECT new UserDto(u.firstname, u.lastname) FROM USER u`.
39
+
<2> Multi-select of `firstname` and `lastname` properties.
40
+
This query gets rewritten to `SELECT new UserDto(u.firstname, u.lastname) FROM USER u`.
41
+
====
42
+
43
+
Repository query methods that return a DTO projection type (a Java type outside the domain type hierarchy) are subject for query rewriting.
44
+
If an `@Query`-annotated query already uses constructor expressions, then Spring Data backs off and doesn't apply DTO constructor expression rewriting.
45
+
46
+
Make sure that your DTO types provide an all-args constructor for the projection, otherwise the query will fail.
0 commit comments