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: documentation/src/main/asciidoc/introduction/Generator.adoc
+32-15Lines changed: 32 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -115,7 +115,13 @@ For example, you could use it to create your own criteria query API.
115
115
Automatic generation of _finder methods_ and _query methods_ is a new feature of Hibernate's implementation of the Metamodel Generator, and an extension to the functionality defined by the JPA specification.
116
116
In this chapter, we're going to explore these features.
117
117
118
-
To whet our appetites, let's see how it works for a `@NamedQuery`.
118
+
We're going to meet three different kinds of generated method:
119
+
120
+
- a _named query method_ has its signature and implementation generated directly from a `@NamedQuery` annotation,
121
+
- a _query method_ has a signature that's explicitly declared, and a generated implementation which executes a HQL or SQL query specified via a `@HQL` or `@SQL` annotation, and
122
+
- a _finder method_ annotated `@Find` has a signature that's explicitly declared, and a generated implementation inferred from the parameter list.
123
+
124
+
To whet our appetites, let's see how this works for a `@NamedQuery`.
119
125
120
126
[[generated-named-queries]]
121
127
=== Named queries and the Metamodel Generator
@@ -128,9 +134,7 @@ Let's just stick it on the `Book` class:
128
134
----
129
135
@CheckHQL // validate the query at compile time
130
136
@NamedQuery(name = "#findByTitleAndType",
131
-
query = "select book from Book book " +
132
-
"where book.title like :titlePattern " +
133
-
" and book.type = :type")
137
+
query = "select book from Book book where book.title like :titlen and book.type = :type")
134
138
@Entity
135
139
public class Book { ... }
136
140
----
@@ -141,11 +145,11 @@ Now the Metamodel Generator adds the following method declaration to the metamod
141
145
.Generated Code
142
146
----
143
147
/**
144
-
* Executes named query {@value #QUERY_FIND_BY_TITLE_AND_TYPE} defined by annotation of {@link Book}.
148
+
* Execute named query {@value #QUERY_FIND_BY_TITLE_AND_TYPE} defined by annotation of {@link Book}.
145
149
**/
146
-
public static List<Book> findByTitleAndType(EntityManager entityManager, String titlePattern, Type type) {
150
+
public static List<Book> findByTitleAndType(@Nonnull EntityManager entityManager, String title, Type type) {
@@ -164,8 +168,8 @@ Now, this is quite nice, but it's a bit inflexible in various ways, and so this
164
168
[[generated-query-methods]]
165
169
=== Generated query methods
166
170
167
-
The problem with generating the query method straight from the `@NamedQuery` annotation is that it doesn't let us explicitly specify the return type or parameter list.
168
-
The Metamodel Generator does a good job of inferring the query return type and parameter types, but we're often going to need a bit more control.
171
+
The principal problem with generating the query method straight from the `@NamedQuery` annotation is that it doesn't let us explicitly specify the return type or parameter list.
172
+
In the case we just saw, the Metamodel Generator does a reasonable job of inferring the query return type and parameter types, but we're often going to need a bit more control.
169
173
170
174
The solution is to write down the signature of the query method _explicitly_, as an abstract method in Java.
171
175
We'll need a place to put this method, and since our `Book` entity isn't an abstract class, we'll just introduce a new interface for this purpose:
@@ -179,7 +183,6 @@ interface Queries {
179
183
----
180
184
181
185
Instead of `@NamedQuery`, which is a type-level annotation, we specify the HQL query using the new `@HQL` annotation, which we place directly on the query method.
182
-
183
186
This results in the following generated code in the `Queries_` class:
184
187
185
188
[source,java]
@@ -209,6 +212,23 @@ public abstract class Queries_ {
209
212
210
213
Notice that the signature differs just slightly from the one we wrote down in the `Queries` interface: the Metamodel Generator has prepended a parameter accepting `EntityManager` to the parameter list.
211
214
215
+
If we want to explicitly specify the name and type of this parameter, we may declare it explicitly:
216
+
217
+
[source,java]
218
+
----
219
+
interface Queries {
220
+
@HQL("from Book where title like :title and type = :type")
0 commit comments