B.4. The Property Factory
In the methods we've been looking at so far, you start
with the criterion, projection, or order you're interested in
creating, and then supply the name of the property with which you want to
work as a parameter. The Criteria API also lets you work in the opposite
direction, starting with the property and then calling a method to build a
criterion or projection based on it. The class
org.hibernate.criterion.Property is a factory for
creating the Property instances you use when you
prefer this approach. Property defines a single
static method, forName( ), which you can
invoke to create an instance representing a particular property. Once
you've got that instance, you can call one of the following methods to
create criteria, projections and orderings based on the property it
represents. Here is a list of the more commonly useful methods; the ones
we omit have to do with detached criteria and subqueries, which are beyond
the scope of this book. When you want to learn about them, see the
Advanced query options chapter of Java
Persistence with Hibernate or Detached
queries and subqueries in the online reference.
Method | Parameters | Purpose |
---|---|---|
asc | None | Creates an Order that will sort results in ascending order by the property. |
avg | None | Creates a Projection that returns the average of the property's values. |
between | Object min, Object max | Creates a Criterion that requires the property have a value between min and max. |
count | None | Creates a Projection that returns the number of times the property appears in the results.[9] |
desc | None | Creates an ordering that will sort results in descending order by the named property. |
eq | Object value | Creates a Criterion that requires the property to equal the supplied value.[10] |
eqProperty | Property other | Creates a Criterion that requires the property to equal another property, represented by other. |
eqProperty | String property | Creates a Criterion that requires the property to equal another property, named by property. |
ge | Object value | Creates a Criterion that requires the property to be greater than or equal to the specified value.[10] |
geProperty | Property other | Creates a Criterion that requires the property to be greater than or equal to another property, represented by other. |
geProperty | String property | Creates a Criterion that requires the property to be greater than or equal to another property, named by property. |
getProperty | String property | Extracts the named component property of the current property (which is presumably a compound property). Returns another Property instance. |
group | None | Creates a Projection that requests results be grouped by values of the property. |
gt | Object value | Creates a Criterion that requires the property to be greater than the specified value.[10] |
gtProperty | Property other | Creates a Criterion that requires the property to be greater than another property, represented by other. |
gtProperty | String property | Creates a Criterion that requires the property to be greater than another property, named by property. |
in | Collection values | Creates a Criterion that requires the supplied collection to contain the property. |
in | Object[] values | Creates a Criterion that requires the property to be equal to any element of the array. |
isEmpty | None | Creates a Criterion that requires the property to be empty (be a collection with length zero). |
isNotEmpty | None | Creates a Criterion that requires the property to be a collection with at least one element. |
isNotNull | None | Creates a Criterion that requires the property to have a non-null value. |
isNull | None | Creates a Criterion that requires the property to have a null value. |
le | Object value | Creates a Criterion that requires the property to be less than or equal to the specified value.[10] |
leProperty | Property other | Creates a Criterion that requires the property to be less than or equal to another property, represented by other. |
leProperty | String property | Creates a Criterion that requires the property to be less than or equal to another property, named by property. |
like | Object value | Creates a Criterion that requires the property to be "like" the specified value (in the sense of the SQL like operator, which allows simple substring matching).[10] |
like | String value, MatchMode mode | A version of "like" for people who don't want to mess with "like" syntax and just want to match a substring. MatchMode is a type-safe enumeration with values START, END, ANYWHERE, and EXACT. This method adjusts the syntax of value to reflect the kind of matching specified by mode, then proceeds like the single-parameter like( ) earlier.[10] |
lt | Object value | Creates a Criterion that requires the property to be less than the specified value.[10] |
ltProperty | Property other | Creates a Criterion that requires the property to be less than another property, represented by other. |
ltProperty | String property | Creates a Criterion that requires the property to be less than another property, named by property. |
max | None | Creates a Projection that returns the largest of the property's values. |
min | None | Creates a Projection that returns the smallest of the property's values. |
ne | Object value | Creates a Criterion that requires the property to have any value other than the specified value.[10] |
neProperty | Property other | Creates a Criterion that requires the property to have a different value than the other. |
neProperty | String property | Creates a Criterion that requires the property to have a different value than the named property. |
[9] The actual class returned,
CountProjection, has a
setDistinct( ) method you can
call if you want to count distinct values.
[10] The actual class returned,
SimpleExpression, has an
ignoreCase( ) method you can call
if you want comparison to be case-insensitive.
No comments:
Post a Comment