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
{{ message }}
This repository was archived by the owner on May 22, 2025. It is now read-only.
Each alias must have a corresponding property or explicit setter on the bean otherwise an exception will be thrown.
184
187
185
188
186
-
==== SQL Projections
189
+
==== SQL Projections are not supported in Hibernate 6
187
190
188
191
189
-
The criteria DSL provides access to Hibernate's SQL projection API.
190
192
191
-
[source,java]
192
-
----
193
-
// Box is a domain class...
194
-
class Box {
195
-
int width
196
-
int height
197
-
}
198
-
----
193
+
==== sqlRestriction have been removed from Hibernate 6
199
194
200
-
[source,java]
201
-
----
202
-
// Use SQL projections to retrieve the perimeter and area of all of the Box instances...
203
-
def c = Box.createCriteria()
204
195
205
-
def results = c.list {
206
-
projections {
207
-
sqlProjection '(2 * (width + height)) as perimeter, (width * height) as area', ['perimeter', 'area'], [INTEGER, INTEGER]
208
-
}
209
-
}
210
-
----
211
-
212
-
The first argument to the `sqlProjection` method is the SQL which defines the projections. The second argument is a list of
213
-
Strings which represent column aliases corresponding to the projected values expressed in the SQL. The third argument
214
-
is a list of `org.hibernate.type.Type` instances which correspond to the projected values expressed in the SQL. The API
215
-
supports all `org.hibernate.type.Type` objects but constants like INTEGER, LONG, FLOAT etc. are provided by the DSL which
216
-
correspond to all of the types defined in `org.hibernate.type.StandardBasicTypes`.
217
196
218
-
Consider that the following table represents the data in the
219
-
`BOX` table.
220
-
[format="csv", options="header"]
221
-
|===
222
197
223
-
width,height
224
-
2,7
225
-
2,8
226
-
2,9
227
-
4,9
228
-
|===
229
-
230
-
The query above would return results like this:
231
-
232
-
[source,groovy]
233
-
----
234
-
[[18, 14], [20, 16], [22, 18], [26, 36]]
235
-
----
236
-
237
-
Each of the inner lists contains the 2 projected values for each `Box`, perimeter and area.
238
-
239
-
NOTE: Note that if there are other references in scope wherever your criteria query is expressed that have names that conflict
240
-
with any of the type constants described above, the code in your criteria will refer to those references, not the type
241
-
constants provided by the DSL. In the unlikely event of that happening you can disambiguate the conflict by referring
242
-
to the fully qualified Hibernate type. For example `StandardBasicTypes.INTEGER` instead of `INTEGER`.
243
-
244
-
If only 1 value is being projected, the alias and the type do not need to be included in a list.
245
-
246
-
[source,java]
247
-
----
248
-
def results = c.list {
249
-
projections {
250
-
sqlProjection 'sum(width * height) as totalArea', 'totalArea', INTEGER
251
-
}
252
-
}
253
-
----
254
-
255
-
That query would return a single result with the value of 84 as the total area of all of the `Box` instances.
256
-
257
-
The DSL supports grouped projections with the `sqlGroupProjection` method.
258
-
259
-
[source,java]
260
-
----
261
-
def results = c.list {
262
-
projections {
263
-
sqlGroupProjection 'width, sum(height) as combinedHeightsForThisWidth', 'width', ['width', 'combinedHeightsForThisWidth'], [INTEGER, INTEGER]
264
-
}
265
-
}
266
-
----
267
-
268
-
The first argument to the `sqlGroupProjection` method is the SQL which defines the projections. The second argument represents the
269
-
group by clause that should be part of the query. That string may be single column name or a comma separated list of column
270
-
names. The third argument is a list of
271
-
Strings which represent column aliases corresponding to the projected values expressed in the SQL. The fourth argument
272
-
is a list of `org.hibernate.type.Type` instances which correspond to the projected values expressed in the SQL.
273
-
274
-
The query above is projecting the combined heights of boxes grouped by width and would return results that look like this:
275
-
276
-
[source,groovy]
277
-
----
278
-
[[2, 24], [4, 9]]
279
-
----
280
-
281
-
Each of the inner lists contains 2 values. The first value is a box width and the second value is the sum of the heights
282
-
of all of the boxes which have that width.
283
-
284
-
285
-
==== Using SQL Restrictions
286
-
287
-
288
-
You can access Hibernate's SQL Restrictions capabilities.
289
-
290
-
[source,java]
291
-
----
292
-
def c = Person.createCriteria()
293
-
294
-
def peopleWithShortFirstNames = c.list {
295
-
sqlRestriction "char_length(first_name) <= 4"
296
-
}
297
-
----
298
-
299
-
SQL Restrictions may be parameterized to deal with SQL injection vulnerabilities related to dynamic restrictions.
300
-
301
-
302
-
[source,java]
303
-
----
304
-
def c = Person.createCriteria()
305
-
306
-
def peopleWithShortFirstNames = c.list {
307
-
sqlRestriction "char_length(first_name) < ? AND char_length(first_name) > ?", [maxValue, minValue]
308
-
}
309
-
----
310
-
311
-
312
-
NOTE: Note that the parameter there is SQL. The `first_name` attribute referenced in the example refers to the persistence model, not the object model like in HQL queries. The `Person` property named `firstName` is mapped to the `first_name` column in the database and you must refer to that in the `sqlRestriction` string.
313
-
314
-
Also note that the SQL used here is not necessarily portable across databases.
315
198
316
199
317
200
==== Using Scrollable Results
@@ -324,13 +207,17 @@ You can use Hibernate's https://docs.jboss.org/hibernate/orm/5.6/javadocs/org/hi
324
207
def results = crit.scroll {
325
208
maxResults(10)
326
209
}
327
-
def f = results.first()
328
-
def l = results.last()
329
-
def n = results.next()
330
-
def p = results.previous()
210
+
results.last()
211
+
m =results.get()
212
+
results.previous()
213
+
j = results.get()
214
+
results.first()
215
+
f = results.get()
216
+
results.next()
217
+
b = results.get()
331
218
332
219
def future = results.scroll(10)
333
-
def accountNumber = results.getLong('number')
220
+
def g = results.get()
334
221
----
335
222
336
223
To quote the documentation of Hibernate ScrollableResults:
@@ -342,21 +229,10 @@ ____
342
229
Contrary to JDBC, columns of results are numbered from zero.
343
230
344
231
345
-
==== Setting properties in the Criteria instance
232
+
==== The Criteria API is not part of Hibernate 6
346
233
347
234
348
-
If a node within the builder tree doesn't match a particular criterion it will attempt to set a property on the Criteria object itself. This allows full access to all the properties in this class. This example calls `setMaxResults` and `setFirstResult` on the https://docs.jboss.org/hibernate/orm/5.6/javadocs/org/hibernate/Criteria.html[Criteria] instance:
0 commit comments