Monday, January 25, 2010

Section B.4. The Property Factory








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.


MethodParametersPurpose

asc

NoneCreates an Order that
will sort results in ascending order by the property.

avg

NoneCreates 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

NoneCreates a Projection
that returns the number of times the property appears in the
results.[9]

desc

NoneCreates an ordering that will sort results in
descending order by the named property.

eq

Object valueCreates a Criterion
that requires the property to equal the supplied value.[10]

eqProperty

Property otherCreates a Criterion
that requires the property to equal another property, represented
by other.

eqProperty
String propertyCreates a Criterion
that requires the property to equal another property, named by
property.

ge

Object valueCreates a Criterion
that requires the property to be greater than or equal to the
specified value.[10]

geProperty

Property otherCreates a Criterion
that requires the property to be greater than or equal to another
property, represented by other.

geProperty
String propertyCreates a Criterion
that requires the property to be greater than or equal to another
property, named by property.

getProperty

String propertyExtracts the named component property of the
current property (which is presumably a compound property).
Returns another Property instance.

group

NoneCreates a Projection
that requests results be grouped by values of the
property.

gt

Object valueCreates a Criterion
that requires the property to be greater than the specified
value.[10]

gtProperty

Property otherCreates a Criterion
that requires the property to be greater than another property,
represented by other.

gtProperty
String propertyCreates a Criterion
that requires the property to be greater than another property,
named by property.

in

Collection valuesCreates a Criterion
that requires the supplied collection to contain the
property.

in
Object[] valuesCreates a Criterion
that requires the property to be equal to any element of the
array.

isEmpty

NoneCreates a Criterion
that requires the property to be empty (be a collection with
length zero).

isNotEmpty

NoneCreates a Criterion
that requires the property to be a collection with at least one
element.

isNotNull

NoneCreates a Criterion
that requires the property to have a non-null value.

isNull

NoneCreates a Criterion
that requires the property to have a null value.

le

Object valueCreates a Criterion
that requires the property to be less than or equal to the
specified value.[10]

leProperty

Property otherCreates a Criterion
that requires the property to be less than or equal to another
property, represented by other.

leProperty
String propertyCreates a Criterion
that requires the property to be less than or equal to another
property, named by property.

like

Object valueCreates 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 valueCreates a Criterion
that requires the property to be less than the specified
value.[10]

ltProperty

Property otherCreates a Criterion
that requires the property to be less than another property,
represented by other.

ltProperty
String propertyCreates a Criterion
that requires the property to be less than another property, named
by property.

max

NoneCreates a Projection
that returns the largest of the property's values.

min

NoneCreates a Projection
that returns the smallest of the property's values.

ne

Object valueCreates a Criterion
that requires the property to have any value other than the
specified value.[10]

neProperty

Property otherCreates a Criterion
that requires the property to have a different value than the
other.

neProperty
String propertyCreates 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: