|
|||||||||||
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, PROJECTION, ROOT_ALIAS, ROOT_ENTITY |
Method Summary | |
Criteria |
add(Criterion criterion)
Add a Criterion to constrain the results to be retrieved. |
Criteria |
addOrder(Order order)
Add an Order to the result set. |
Criteria |
createAlias(String associationPath,
String alias)
Join an association, assigning an alias to the joined entity |
Criteria |
createCriteria(String associationPath)
Create a new Criteria, "rooted" at the associated entity |
Criteria |
createCriteria(String associationPath,
String alias)
Create a new Criteria, "rooted" at the associated entity, assigning the given alias |
String |
getAlias()
Get the alias of the entity |
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. |
Criteria |
setCacheable(boolean cacheable)
Enable caching of this query result set |
Criteria |
setCacheMode(CacheMode cacheMode)
Override the cache mode |
Criteria |
setCacheRegion(String cacheRegion)
Set the name of the cache region. |
Criteria |
setComment(String comment)
Add a comment to the generated SQL |
Criteria |
setFetchMode(String associationPath,
FetchMode mode)
Specify an association fetching strategy for a one-to-many, many-to-one or one-to-one association, or for 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 |
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)
Set a projection of projection list, and select the PROJECTION result transformer |
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 Criteria add(Criterion criterion)
criterion
-
public Criteria addOrder(Order order)
order
-
public Criteria setFetchMode(String associationPath, FetchMode mode) throws HibernateException
associationPath
- a dot seperated property pathmode
- the fetch mode
HibernateException
public Criteria createAlias(String associationPath, String alias) throws HibernateException
HibernateException
public Criteria createCriteria(String associationPath) throws HibernateException
HibernateException
public Criteria createCriteria(String associationPath, String alias) throws HibernateException
HibernateException
public Criteria setProjection(Projection projection)
public String getAlias()
public Criteria setResultTransformer(ResultTransformer resultTransformer)
resultTransformer
- CriteriaSpecification.ROOT_ENTITY
,
CriteriaSpecification.DISTINCT_ROOT_ENTITY
,
CriteriaSpecification.ALIAS_TO_ENTITY_MAP
public Criteria setMaxResults(int maxResults)
maxResults
- the maximum number of results
public Criteria setFirstResult(int firstResult)
firstResult
- the first result, numbered from 0
public Criteria setFetchSize(int fetchSize)
fetchSize
- the fetch sizepublic Criteria setTimeout(int timeout)
timeout
-
public Criteria setCacheable(boolean cacheable)
public Criteria setCacheRegion(String cacheRegion)
cacheRegion
- the name of a query cache region, or null
for the default query cachepublic List list() throws HibernateException
HibernateException
public ScrollableResults scroll() throws HibernateException
HibernateException
public ScrollableResults scroll(ScrollMode scrollMode) throws HibernateException
HibernateException
public Object uniqueResult() throws HibernateException
HibernateException
- if there is more than one matching resultpublic Criteria setLockMode(LockMode lockMode)
lockMode
- the lock modepublic Criteria setLockMode(String alias, LockMode lockMode)
alias
- an aliaslockMode
- the lock modepublic Criteria setComment(String comment)
comment
- a human-readable stringpublic Criteria setFlushMode(FlushMode flushMode)
public Criteria setCacheMode(CacheMode cacheMode)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |