Skip to content

Commit 5b719c3

Browse files
committed
more prominent discussion of result types
1 parent aefab46 commit 5b719c3

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ private RuntimeException convertNamedQueryException(RuntimeException e) {
11621162
// it also expects an IllegalArgumentException, so wrap UnknownNamedQueryException
11631163
return new IllegalArgumentException( e.getMessage(), e );
11641164
}
1165-
else if ( e instanceof IllegalArgumentException) {
1165+
else if ( e instanceof IllegalArgumentException ) {
11661166
return e;
11671167
}
11681168
else {

hibernate-core/src/main/java/org/hibernate/query/QueryProducer.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,41 @@
1818
* {@link jakarta.persistence.EntityManager}. They are declared here to allow reuse by
1919
* {@code StatelessSession}.
2020
* <p>
21-
* Unlike the corresponding operations of {@code EntityManager}, operations for creating untyped
22-
* instances of {@code Query} are all marked as deprecated. Clients must migrate to the use of
23-
* the equivalent operations which accept a {@link Class} and return a typed {@code Query}.
21+
* Operations like {@link #createQuery(String, Class)}, {@link #createNamedQuery(String, Class)},
22+
* and {@link #createNativeQuery(String, Class)} accept an instance indicating the return type
23+
* of the query.
24+
* <ul>
25+
* <li>The result type might be an {@linkplain jakarta.persistence.Entity entity} class, when
26+
* the query returns an entity:
27+
* <pre>
28+
* List&lt;Book&gt; allBooks =
29+
* session.createNativeQuery("select * from books order by title", Book.class)
30+
* .getResultList();
31+
* </pre>
32+
* <li>It might be a {@linkplain org.hibernate.type.descriptor.java.JavaType basic type} like
33+
* {@code String} or {@code Long}:
34+
* <pre>
35+
* List&lt;String&gt; allTitles =
36+
* session.createNativeQuery("select distinct title from books order by title", String.class)
37+
* .getResultList();
38+
* </pre>
39+
* <li>Finally, the result type might be a class used to package the elements of a {@code select}
40+
* list, such as a Java record with an appropriate constructor, {@code Map}, {@code List}, or
41+
* {@code Object[]}:
42+
* <pre>
43+
* record IsbnAndTitle(String isbn, String title) {}
44+
*
45+
* List&lt;IsbnAndTitle&gt; allBooks =
46+
* session.createNativeQuery("select isbn, title from books order by title", IsbnAndTitle.class)
47+
* .getResultList();
48+
* </pre>
49+
* </ul>
50+
* For a {@linkplain #createQuery(CriteriaQuery) criteria query}, the result type is already
51+
* determined by {@link CriteriaQuery#getResultType()}.
52+
*
53+
* @apiNote Unlike the corresponding operations of {@code EntityManager}, operations for creating
54+
* untyped instances of {@code Query} are all marked as deprecated. Clients must migrate to the
55+
* use of the equivalent operations which accept a {@link Class} and return a typed {@code Query}.
2456
*
2557
* @author Steve Ebersole
2658
*/

0 commit comments

Comments
 (0)