Skip to content

Commit

Permalink
Update Projection section in reference documentation.
Browse files Browse the repository at this point in the history
For the time being we at least now document the current behaviour and how to cope with it.

See: #3286
  • Loading branch information
christophstrobl committed Apr 26, 2024
1 parent cde6bf7 commit 9a16062
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/antora/modules/ROOT/pages/repositories/projections.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,28 @@
include::{commons}@data-commons::page$repositories/projections.adoc[leveloffset=+1]

NOTE: It is important to note that <<projections.dtos,Class-based projections>> with JPQL is limited to *constructor expressions* in your JPQL expression, e.g. `SELECT new com.example.NamesOnly(u.firstname, u.lastname) from User u`. (Note the usage of a FQDN for the DTO type!) This JPQL expression can be used in `@Query` annotations as well where you define any named queries. And it's important to point out that class-based projections do not work with native queries AT ALL. As a workaround you may use named queries with `ResultSetMapping` or the Hibernate specific https://docs.jboss.org/hibernate/orm/6.0/javadocs/org/hibernate/transform/ResultTransformer.html[`ResultTransformer`]

[NOTE]
====
Some JPA providers may require additional hints when working with <<projections.dtos,Class-based projections>> especially when using those directly with method derived queries.
If you are facing errors like `JpaSystemException: Specified result type did not match Query selection type` make sure to provide a constructor hint via `@PersistenceCreator` to be passed on, as outlined below:
[source,java]
----
public class NamesOnly {
private final String firstname;
private final String lastname;
protected NamesOnly() { }
@PersistenceCreator
public NamesOnly(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
// ...
}
----
====

0 comments on commit 9a16062

Please sign in to comment.