Skip to content

Commit 7f3c73b

Browse files
committed
update processor chapter for JPA 3.2
Signed-off-by: Gavin King <[email protected]>
1 parent 5170561 commit 7f3c73b

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

documentation/src/main/asciidoc/introduction/Hibernate_Introduction.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ include::Configuration.adoc[]
2020
include::Entities.adoc[]
2121
include::Mapping.adoc[]
2222
include::Interacting.adoc[]
23-
include::Generator.adoc[]
23+
include::Processor.adoc[]
2424
include::Tuning.adoc[]
2525
include::Advanced.adoc[]
2626
include::Credits.adoc[]

documentation/src/main/asciidoc/introduction/Introduction.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ This "test" is one which many people like to run even in production, when the sy
768768

769769
It's now time to begin our journey toward actually _understanding_ the code we saw earlier.
770770

771+
This introduction will guide you through the basic tasks involved in developing a program that uses Hibernate for persistence:
771772
This introduction will guide you through the basic tasks involved in developing a program that uses Hibernate for persistence:
772773

773774
1. configuring and bootstrapping Hibernate, and obtaining an instance of `SessionFactory` or `EntityManagerFactory`,

documentation/src/main/asciidoc/introduction/Generator.adoc renamed to documentation/src/main/asciidoc/introduction/Processor.adoc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,56 @@ This is very useful for writing generic code in frameworks or libraries.
116116
For example, you could use it to create your own criteria query API.
117117
====
118118

119+
The JPA static metamodel for an entity also contains members representing the named queries and named entity graphs declared by `@NamedQuery`, `@NamedNativeQuery`, and `@NamedEntityGraph` annotations of the entity class.
120+
121+
For example, if we had:
122+
123+
[source,java]
124+
----
125+
@CheckHQL // validate named queries at compile time
126+
@NamedQuery(name="findBooksByTitle",
127+
query="from Book where title like :title order by title")
128+
@Entity
129+
class Book { ... }
130+
----
131+
132+
Then we may execute the query as follows:
133+
134+
[source,java]
135+
----
136+
var books =
137+
entityManager.createNamedQuery(Queries_._findBooksByTitle_)
138+
.setParameter("title", titlePattern)
139+
.setPage(page)
140+
.getResultList();
141+
----
142+
143+
Notice that no typecast was required here, since the generated code embeds the return type of the query as a type argument of the JPA `TypedQueryReference`:
144+
145+
[source,java]
146+
----
147+
/**
148+
* @see #_findBooksByTitle_
149+
**/
150+
public static final String QUERY_FIND_BOOKS_BY_TITLE = "findBooksByTitle";
151+
152+
153+
/**
154+
* The query named {@value QUERY_FIND_BOOKS_BY_TITLE}
155+
* <pre>
156+
* from Book where title like :title order by title
157+
* </pre>
158+
*
159+
* @see org.example.Book
160+
**/
161+
public static volatile TypedQueryReference<Book> _findBooksByTitle_;
162+
----
163+
164+
[TIP]
165+
====
166+
Actually, Hibernate Processor doesn't require that such annotations be applied to the entity class itself, as we <<organizing-persistence,already saw earlier>>.
167+
====
168+
119169
We've already been using metamodel references like `Book_.authors` and `Book.AUTHORS` in the previous chapters.
120170
So now let's see what else Hibernate Processor can do for us.
121171

0 commit comments

Comments
 (0)