criteria用法
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
| SUMMARY:NESTED|FIELD|CONSTR|METHOD | DETAIL:FIELD|CONSTR|METHOD | |||||||||
org.hibernate
Interface Criteria
All Superinterfaces:CriteriaSpecificationAll Known Implementing Classes:CriteriaImpl,CriteriaImpl.Subcriteria
public interface Criteriaextends CriteriaSpecification
Criteriais a simplified API for retrieving entities by composingCriterionobjects. 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.
TheSessionis a factory forCriteria.Criterioninstances are usually obtained via the factory methods onRestrictions. 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 usingcreateAlias()orcreateCriteria().
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 usingProjectioninstances obtained via the factory methods onProjections.
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();
Author:Gavin KingSee Also: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(Criterioncriterion)Add a restrictionto constrain the results to be retrieved. |
Criteria |
addOrder(Orderorder)Add an orderingto the result set. |
Criteria |
createAlias(StringassociationPath,Stringalias)Join an association, assigning an alias to the joined association. |
Criteria |
createAlias(StringassociationPath,Stringalias, intjoinType)Join an association using the specified join-type, assigning an alias to the joined association. |
Criteria |
createAlias(StringassociationPath,Stringalias, intjoinType,CriterionwithClause)Join an association using the specified join-type, assigning an alias to the joined association. |
Criteria |
createCriteria(StringassociationPath)Create a newCriteria, "rooted" at the associated entity. |
Criteria |
createCriteria(StringassociationPath, intjoinType)Create a newCriteria, "rooted" at the associated entity, using the specified join type. |
Criteria |
createCriteria(StringassociationPath,Stringalias)Create a newCriteria, "rooted" at the associated entity, assigning the given alias. |
Criteria |
createCriteria(StringassociationPath,Stringalias, intjoinType)Create a newCriteria, "rooted" at the associated entity, assigning the given alias and using the specified join type. |
Criteria |
createCriteria(StringassociationPath,Stringalias, intjoinType,CriterionwithClause)Create a newCriteria, "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. |
boolean |
isReadOnly()Should entities and proxies loaded by this Criteria be put in read-only mode? If the read-only/modifiable setting was not initialized, then the default read-only/modifiable setting for the persistence context is returned instead. |
boolean |
isReadOnlyInitialized()Was the read-only/modifiable mode explicitly initialized? |
List |
list()Get the results. |
ScrollableResults |
scroll()Get the results as an instance of ScrollableResults |
ScrollableResults |
scroll(ScrollModescrollMode)Get the results as an instance of ScrollableResultsbased on the given scroll mode. |
Criteria |
setCacheable(booleancacheable)Enable caching of this query result, provided query caching is enabled for the underlying session factory. |
Criteria |
setCacheMode(CacheModecacheMode)Override the cache mode for this particular query. |
Criteria |
setCacheRegion(StringcacheRegion)Set the name of the cache region to use for query result caching. |
Criteria |
setComment(Stringcomment)Add a comment to the generated SQL. |
Criteria |
setFetchMode(StringassociationPath,FetchModemode)Specify an association fetching strategy for an association or a collection of values. |
Criteria |
setFetchSize(intfetchSize)Set a fetch size for the underlying JDBC query. |
Criteria |
setFirstResult(intfirstResult)Set the first result to be retrieved. |
Criteria |
setFlushMode(FlushModeflushMode)Override the flush mode for this particular query. |
Criteria |
setLockMode(LockModelockMode)Set the lock mode of the current entity |
Criteria |
setLockMode(Stringalias,LockModelockMode)Set the lock mode of the aliased entity |
Criteria |
setMaxResults(intmaxResults)Set a limit upon the number of objects to be retrieved. |
Criteria |
setProjection(Projectionprojection)Used to specify that the query results will be a projection (scalar in nature). |
Criteria |
setReadOnly(booleanreadOnly)Set the read-only/modifiable mode for entities and proxies loaded by this Criteria. |
Criteria |
setResultTransformer(ResultTransformerresultTransformer)Set a strategy for handling the query results. |
Criteria |
setTimeout(inttimeout)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 |
|---|
getAlias
String getAlias()
Get the alias of the entity encapsulated by this criteria instance.
Returns:The alias for the encapsulated entity.
setProjection
Criteria setProjection(Projectionprojection)
Used to specify that the query results will be a projection (scalar in nature). Implicitly specifies theCriteriaSpecification.PROJECTIONresult transformer.
The individual components contained within the givenprojectiondetermines the overall "shape" of the query result.
Parameters:projection- The projection representing the overall "shape" of the query results.Returns:this (for method chaining)
add
Criteria add(Criterioncriterion)
Add arestrictionto constrain the results to be retrieved.
Parameters:criterion- Thecriterionobject representing the restriction to be applied.Returns:this (for method chaining)
addOrder
Criteria addOrder(Orderorder)
Add anorderingto the result set.
Parameters:order- Theorderobject representing an ordering to be applied to the results.Returns:this (for method chaining)
setFetchMode
Criteria setFetchMode(StringassociationPath,
FetchModemode)
throws HibernateException
Specify an association fetching strategy for an association or a collection of values.
Parameters:associationPath- a dot seperated property pathmode- The fetch mode for the referenced associationReturns:this (for method chaining)Throws:HibernateException- Indicates a problem applying the given fetch mode
setLockMode
Criteria setLockMode(LockModelockMode)
Set the lock mode of the current entity
Parameters:lockMode- The lock mode to be appliedReturns:this (for method chaining)
setLockMode
Criteria setLockMode(Stringalias,
LockModelockMode)
Set the lock mode of the aliased entity
Parameters:alias- The previously assigned alias representing the entity to which the given lock mode should apply.lockMode- The lock mode to be appliedReturns:this (for method chaining)
createAlias
Criteria createAlias(StringassociationPath,
Stringalias)
throws HibernateException
Join an association, assigning an alias to the joined association.
Functionally equivalent tocreateAlias(String, String, int)usingCriteriaSpecification.INNER_JOINfor the joinType.
Parameters:associationPath- A dot-seperated property pathalias- The alias to assign to the joined association (for later reference).Returns:this (for method chaining)Throws:HibernateException- Indicates a problem creating the sub criteria
createAlias
Criteria createAlias(StringassociationPath,
Stringalias,
intjoinType)
throws HibernateException
Join an association using the specified join-type, assigning an alias to the joined association.
The joinType is expected to be one ofCriteriaSpecification.INNER_JOIN(the default),CriteriaSpecification.FULL_JOIN, orCriteriaSpecification.LEFT_JOIN.
Parameters:associationPath- A dot-seperated property pathalias- The alias to assign to the joined association (for later reference).joinType- The type of join to use.Returns:this (for method chaining)Throws:HibernateException- Indicates a problem creating the sub criteria
createAlias
Criteria createAlias(StringassociationPath,
Stringalias,
intjoinType,
CriterionwithClause)
throws HibernateException
Join an association using the specified join-type, assigning an alias to the joined association.
The joinType is expected to be one ofCriteriaSpecification.INNER_JOIN(the default),CriteriaSpecification.FULL_JOIN, orCriteriaSpecification.LEFT_JOIN.
Parameters:associationPath- A dot-seperated property pathalias- The alias to assign to the joined association (for later reference).joinType- The type of join to use.withClause- The criteria to be added to the join condition (ONclause)Returns:this (for method chaining)Throws:HibernateException- Indicates a problem creating the sub criteria
createCriteria
Criteria createCriteria(StringassociationPath)
throws HibernateException
Create a newCriteria, "rooted" at the associated entity.
Functionally equivalent tocreateCriteria(String, int)usingCriteriaSpecification.INNER_JOINfor the joinType.
Parameters:associationPath- A dot-seperated property pathReturns:the created "sub criteria"Throws:HibernateException- Indicates a problem creating the sub criteria
createCriteria
Criteria createCriteria(StringassociationPath,
intjoinType)
throws HibernateException
Create a newCriteria, "rooted" at the associated entity, using the specified join type.
Parameters:associationPath- A dot-seperated property pathjoinType- The type of join to use.Returns:the created "sub criteria"Throws:HibernateException- Indicates a problem creating the sub criteria
createCriteria
Criteria createCriteria(StringassociationPath,
Stringalias)
throws HibernateException
Create a newCriteria, "rooted" at the associated entity, assigning the given alias.
Functionally equivalent tocreateCriteria(String, String, int)usingCriteriaSpecification.INNER_JOINfor the joinType.
Parameters:associationPath- A dot-seperated property pathalias- The alias to assign to the joined association (for later reference).Returns:the created "sub criteria"Throws:HibernateException- Indicates a problem creating the sub criteria
createCriteria
Criteria createCriteria(StringassociationPath,
Stringalias,
intjoinType)
throws HibernateException
Create a newCriteria, "rooted" at the associated entity, assigning the given alias and using the specified join type.
Parameters:associationPath- A dot-seperated property pathalias- The alias to assign to the joined association (for later reference).joinType- The type of join to use.Returns:the created "sub criteria"Throws:HibernateException- Indicates a problem creating the sub criteria
createCriteria
Criteria createCriteria(StringassociationPath,
Stringalias,
intjoinType,
CriterionwithClause)
throws HibernateException
Create a newCriteria, "rooted" at the associated entity, assigning the given alias and using the specified join type.
Parameters:associationPath- A dot-seperated property pathalias- The alias to assign to the joined association (for later reference).joinType- The type of join to use.withClause- The criteria to be added to the join condition (ONclause)Returns:the created "sub criteria"Throws:HibernateException- Indicates a problem creating the sub criteria
setResultTransformer
Criteria setResultTransformer(ResultTransformerresultTransformer)
Set a strategy for handling the query results. This determines the "shape" of the query result.
Parameters:resultTransformer- The transformer to applyReturns:this (for method chaining)See Also:CriteriaSpecification.ROOT_ENTITY,CriteriaSpecification.DISTINCT_ROOT_ENTITY,CriteriaSpecification.ALIAS_TO_ENTITY_MAP,CriteriaSpecification.PROJECTION
setMaxResults
Criteria setMaxResults(intmaxResults)
Set a limit upon the number of objects to be retrieved.
Parameters:maxResults- the maximum number of resultsReturns:this (for method chaining)
setFirstResult
Criteria setFirstResult(intfirstResult)
Set the first result to be retrieved.
Parameters:firstResult- the first result to retrieve, numbered from0Returns:this (for method chaining)
isReadOnlyInitialized
boolean isReadOnlyInitialized()
Was the read-only/modifiable mode explicitly initialized?
Returns:true, the read-only/modifiable mode was explicitly initialized; false, otherwise.See Also:setReadOnly(boolean)
isReadOnly
boolean isReadOnly()
Should entities and proxies loaded by this Criteria be put in read-only mode? If the read-only/modifiable setting was not initialized, then the default read-only/modifiable setting for the persistence context is returned instead.
Returns:true, entities and proxies loaded by the criteria will be put in read-only mode false, entities and proxies loaded by the criteria will be put in modifiable modeThrows:IllegalStateException- ifisReadOnlyInitialized()returnsfalseand this Criteria is not associated with a session.See Also:setReadOnly(boolean),The read-only/modifiable setting has no impact on entities/proxies returned by the Criteria that existed in the session before the Criteria was executed.,isReadOnlyInitialized()
setReadOnly
Criteria setReadOnly(booleanreadOnly)
Set the read-only/modifiable mode for entities and proxies loaded by this Criteria. This setting overrides the default setting for the persistence context.
Parameters:readOnly- true, entities and proxies loaded by the criteria will be put in read-only mode false, entities and proxies loaded by the criteria will be put in modifiable modeSee Also:To set the default read-only/modifiable setting used for entities and proxies that are loaded into the session:,PersistenceContext.setDefaultReadOnly(boolean),Read-only entities are not dirty-checked and snapshots of persistent state are not maintained. Read-only entities can be modified, but changes are not persisted. When a proxy is initialized, the loaded entity will have the same read-only/modifiable setting as the uninitialized proxy has, regardless of the session's current setting. The read-only/modifiable setting has no impact on entities/proxies returned by the criteria that existed in the session before the criteria was executed.
setFetchSize
Criteria setFetchSize(intfetchSize)
Set a fetch size for the underlying JDBC query.
Parameters:fetchSize- the fetch sizeReturns:this (for method chaining)See Also:Statement.setFetchSize(int)
setTimeout
Criteria setTimeout(inttimeout)
Set a timeout for the underlying JDBC query.
Parameters:timeout- The timeout value to apply.Returns:this (for method chaining)See Also:Statement.setQueryTimeout(int)
setCacheable
Criteria setCacheable(booleancacheable)
Enable caching of this query result, provided query caching is enabled for the underlying session factory.
Parameters:cacheable- Should the result be considered cacheable; default is to not cache (false).Returns:this (for method chaining)
setCacheRegion
Criteria setCacheRegion(StringcacheRegion)
Set the name of the cache region to use for query result caching.
Parameters:cacheRegion- the name of a query cache region, ornullfor the default query cacheReturns:this (for method chaining)See Also:setCacheable(boolean)
setComment
Criteria setComment(Stringcomment)
Add a comment to the generated SQL.
Parameters:comment- a human-readable stringReturns:this (for method chaining)
setFlushMode
Criteria setFlushMode(FlushModeflushMode)
Override the flush mode for this particular query.
Parameters:flushMode- The flush mode to use.Returns:this (for method chaining)
setCacheMode
Criteria setCacheMode(CacheModecacheMode)
Override the cache mode for this particular query.
Parameters:cacheMode- The cache mode to use.Returns:this (for method chaining)
list
List list()
throws HibernateException
Get the results.
Returns:The list of matched query results.Throws:HibernateException- Indicates a problem either translating the criteria to SQL, exeucting the SQL or processing the SQL results.
scroll
ScrollableResults scroll()
throws HibernateException
Get the results as an instance ofScrollableResults
Returns:TheScrollableResultsrepresenting the matched query results.Throws:HibernateException- Indicates a problem either translating the criteria to SQL, exeucting the SQL or processing the SQL results.
scroll
ScrollableResults scroll(ScrollModescrollMode)
throws HibernateException
Get the results as an instance ofScrollableResultsbased on the given scroll mode.
Parameters:scrollMode- Indicates the type of underlying database cursor to request.Returns:TheScrollableResultsrepresenting the matched query results.Throws:HibernateException- Indicates a problem either translating the criteria to SQL, exeucting the SQL or processing the SQL results.
uniqueResult
Object uniqueResult()
throws HibernateException
Convenience method to return a single instance that matches the query, or null if the query returns no results.
Returns:the single result ornullThrows:HibernateException- if there is more than one matching result
總結
以上是生活随笔為你收集整理的criteria用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 颜色的写法
- 下一篇: 食品公司名字大全好听大气,好听的食品公司