Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit cf499cb

Browse files
committed
implemented tests for what is documented in criteria.adoc
1 parent 6be6c3a commit cf499cb

File tree

7 files changed

+368
-220
lines changed

7 files changed

+368
-220
lines changed

docs/src/docs/asciidoc/querying/criteria.adoc

Lines changed: 18 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@ And you can also negate using logical NOT:
5858
[source,java]
5959
----
6060
not {
61-
between("balance", 500, 1000)
62-
eq("branch", "London")
61+
and{
62+
between("balance", 500, 1000)
63+
eq("branch", "London")
64+
}
65+
6366
}
6467
----
6568

66-
All top level conditions are implied to be AND'd together.
69+
In previous release all top level conditions weere implied to be AND'd together, but this is not supported anymore
6770

6871

6972
==== Querying Associations
@@ -183,135 +186,15 @@ def accountsOverview = c.get {
183186
Each alias must have a corresponding property or explicit setter on the bean otherwise an exception will be thrown.
184187

185188

186-
==== SQL Projections
189+
==== SQL Projections are not supported in Hibernate 6
187190

188191

189-
The criteria DSL provides access to Hibernate's SQL projection API.
190192

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
199194

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()
204195

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`.
217196

218-
Consider that the following table represents the data in the
219-
`BOX` table.
220-
[format="csv", options="header"]
221-
|===
222197

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.
315198

316199

317200
==== Using Scrollable Results
@@ -324,13 +207,17 @@ You can use Hibernate's https://docs.jboss.org/hibernate/orm/5.6/javadocs/org/hi
324207
def results = crit.scroll {
325208
maxResults(10)
326209
}
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()
331218
332219
def future = results.scroll(10)
333-
def accountNumber = results.getLong('number')
220+
def g = results.get()
334221
----
335222

336223
To quote the documentation of Hibernate ScrollableResults:
@@ -342,21 +229,10 @@ ____
342229
Contrary to JDBC, columns of results are numbered from zero.
343230

344231

345-
==== Setting properties in the Criteria instance
232+
==== The Criteria API is not part of Hibernate 6
346233

347234

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:
349235

350-
[source,java]
351-
----
352-
import org.hibernate.FetchMode as FM
353-
...
354-
def results = c.list {
355-
maxResults(10)
356-
firstResult(50)
357-
fetchMode("aRelationship", FM.JOIN)
358-
}
359-
----
360236

361237

362238
==== Querying with Eager Fetching

0 commit comments

Comments
 (0)