|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Criteria is a simplified API for retrieving entities
by composing Criterion objects. This is a very
convenient approach for functionality like "search" screens
where there is a variable number of conditions to be placed
upon the result set.
The Session is a factory for Criteria.
Criterion instances are usually obtained via
the factory methods on Restrictions. eg.
List cats = session.createCriteria(Cat.class) .add( Restrictions.like("name", "Iz%") ) .add( Restrictions.gt( "weight", new Float(minWeight) ) ) .addOrder( Order.asc("age") ) .list();You may navigate associations using createAlias() or createCriteria().
List cats = session.createCriteria(Cat.class) .createCriteria("kittens") .add( Restrictions.like("name", "Iz%") ) .list();
List cats = session.createCriteria(Cat.class) .createAlias("kittens", "kit") .add( Restrictions.like("kit.name", "Iz%") ) .list();You may specify projection and aggregation using Projection instances obtained via the factory methods on Projections.
List cats = session.createCriteria(Cat.class) .setProjection( Projections.projectionList() .add( Projections.rowCount() ) .add( Projections.avg("weight") ) .add( Projections.max("weight") ) .add( Projections.min("weight") ) .add( Projections.groupProperty("color") ) ) .addOrder( Order.asc("color") ) .list();
Session.createCriteria(java.lang.Class)
,
Restrictions
,
Projections
,
Order
,
Criterion
,
Projection
,
a disconnected version of this API
Field Summary |
Fields inherited from interface org.hibernate.criterion.CriteriaSpecification |
ALIAS_TO_ENTITY_MAP, DISTINCT_ROOT_ENTITY, FULL_JOIN, INNER_JOIN, LEFT_JOIN, PROJECTION, ROOT_ALIAS, ROOT_ENTITY |
Method Summary | |
Criteria |
add(Criterion criterion)
Add a restriction to constrain the results to be
retrieved. |
Criteria |
addOrder(Order order)
Add an ordering to the result set. |
Criteria |
createAlias(String associationPath,
String alias)
Join an association, assigning an alias to the joined association. |
Criteria |
createAlias(String associationPath,
String alias,
int joinType)
Join an association using the specified join-type, assigning an alias to the joined association. |
Criteria |
createCriteria(String associationPath)
Create a new Criteria, "rooted" at the associated entity. |
Criteria |
createCriteria(String associationPath,
int joinType)
Create a new Criteria, "rooted" at the associated entity, using the specified join type. |
Criteria |
createCriteria(String associationPath,
String alias)
Create a new Criteria, "rooted" at the associated entity, assigning the given alias. |
Criteria |
createCriteria(String associationPath,
String alias,
int joinType)
Create a new Criteria, "rooted" at the associated entity, assigning the given alias and using the specified join type. |
String |
getAlias()
Get the alias of the entity encapsulated by this criteria instance. |
List |
list()
Get the results. |
ScrollableResults |
scroll()
Get the results as an instance of ScrollableResults |
ScrollableResults |
scroll(ScrollMode scrollMode)
Get the results as an instance of ScrollableResults based on the
given scroll mode. |
Criteria |
setCacheable(boolean cacheable)
Enable caching of this query result, provided query caching is enabled for the underlying session factory. |
Criteria |
setCacheMode(CacheMode cacheMode)
Override the cache mode for this particular query. |
Criteria |
setCacheRegion(String cacheRegion)
Set the name of the cache region to use for query result caching. |
Criteria |
setComment(String comment)
Add a comment to the generated SQL. |
Criteria |
setFetchMode(String associationPath,
FetchMode mode)
Specify an association fetching strategy for an association or a collection of values. |
Criteria |
setFetchSize(int fetchSize)
Set a fetch size for the underlying JDBC query. |
Criteria |
setFirstResult(int firstResult)
Set the first result to be retrieved. |
Criteria |
setFlushMode(FlushMode flushMode)
Override the flush mode for this particular query. |
Criteria |
setLockMode(LockMode lockMode)
Set the lock mode of the current entity |
Criteria |
setLockMode(String alias,
LockMode lockMode)
Set the lock mode of the aliased entity |
Criteria |
setMaxResults(int maxResults)
Set a limit upon the number of objects to be retrieved. |
Criteria |
setProjection(Projection projection)
Used to specify that the query results will be a projection (scalar in nature). |
Criteria |
setResultTransformer(ResultTransformer resultTransformer)
Set a strategy for handling the query results. |
Criteria |
setTimeout(int timeout)
Set a timeout for the underlying JDBC query. |
Object |
uniqueResult()
Convenience method to return a single instance that matches the query, or null if the query returns no results. |
Method Detail |
public String getAlias()
public Criteria setProjection(Projection projection)
CriteriaSpecification.PROJECTION
result transformer.
The individual components contained within the given
projection
determines the overall "shape" of the
query result.
projection
- The projection representing the overall "shape" of the
query results.
public Criteria add(Criterion criterion)
restriction
to constrain the results to be
retrieved.
criterion
- The criterion
object representing the
restriction to be applied.
public Criteria addOrder(Order order)
ordering
to the result set.
order
- The order
object representing an ordering
to be applied to the results.
public Criteria setFetchMode(String associationPath, FetchMode mode) throws HibernateException
associationPath
- a dot seperated property pathmode
- The fetch mode for the referenced association
HibernateException
public Criteria setLockMode(LockMode lockMode)
lockMode
- The lock mode to be applied
public Criteria setLockMode(String alias, LockMode lockMode)
alias
- The previously assigned alias representing the entity to
which the given lock mode should apply.lockMode
- The lock mode to be applied
public Criteria createAlias(String associationPath, String alias) throws HibernateException
createAlias(String, String, int)
using
CriteriaSpecification.INNER_JOIN
for the joinType.
associationPath
- A dot-seperated property pathalias
- The alias to assign to the joined association (for later reference).
HibernateException
public Criteria createAlias(String associationPath, String alias, int joinType) throws HibernateException
CriteriaSpecification.INNER_JOIN
(the default),
CriteriaSpecification.FULL_JOIN
, or CriteriaSpecification.LEFT_JOIN
.
associationPath
- A dot-seperated property pathalias
- The alias to assign to the joined association (for later reference).joinType
- The type of join to use.
HibernateException
public Criteria createCriteria(String associationPath) throws HibernateException
createCriteria(String, int)
using
CriteriaSpecification.INNER_JOIN
for the joinType.
associationPath
- A dot-seperated property path
HibernateException
public Criteria createCriteria(String associationPath, int joinType) throws HibernateException
associationPath
- A dot-seperated property pathjoinType
- The type of join to use.
HibernateException
public Criteria createCriteria(String associationPath, String alias) throws HibernateException
createCriteria(String, String, int)
using
CriteriaSpecification.INNER_JOIN
for the joinType.
associationPath
- A dot-seperated property pathalias
- The alias to assign to the joined association (for later reference).
HibernateException
public Criteria createCriteria(String associationPath, String alias, int joinType) throws HibernateException
associationPath
- A dot-seperated property pathalias
- The alias to assign to the joined association (for later reference).joinType
- The type of join to use.
HibernateException
public Criteria setResultTransformer(ResultTransformer resultTransformer)
resultTransformer
- The transformer to apply
CriteriaSpecification.ROOT_ENTITY
,
CriteriaSpecification.DISTINCT_ROOT_ENTITY
,
CriteriaSpecification.ALIAS_TO_ENTITY_MAP
,
CriteriaSpecification.PROJECTION
public Criteria setMaxResults(int maxResults)
maxResults
- the maximum number of results
public Criteria setFirstResult(int firstResult)
firstResult
- the first result to retrieve, numbered from 0
public Criteria setFetchSize(int fetchSize)
fetchSize
- the fetch size
Statement.setFetchSize(int)
public Criteria setTimeout(int timeout)
timeout
- The timeout value to apply.
Statement.setQueryTimeout(int)
public Criteria setCacheable(boolean cacheable)
cacheable
- Should the result be considered cacheable; default is
to not cache (false).
public Criteria setCacheRegion(String cacheRegion)
cacheRegion
- the name of a query cache region, or null
for the default query cache
setCacheable(boolean)
public Criteria setComment(String comment)
comment
- a human-readable string
public Criteria setFlushMode(FlushMode flushMode)
flushMode
- The flush mode to use.
public Criteria setCacheMode(CacheMode cacheMode)
cacheMode
- The cache mode to use.
public List list() throws HibernateException
HibernateException
public ScrollableResults scroll() throws HibernateException
ScrollableResults
ScrollableResults
representing the matched
query results.
HibernateException
public ScrollableResults scroll(ScrollMode scrollMode) throws HibernateException
ScrollableResults
based on the
given scroll mode.
scrollMode
- Indicates the type of underlying database cursor to
request.
ScrollableResults
representing the matched
query results.
HibernateException
public Object uniqueResult() throws HibernateException
HibernateException
- if there is more than one matching result
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |