按条件方式查询QBC
Query By Criteria,Hibernate提供了Criteria接口类为开发者在运行时建立查询条件,开发者可以动态的建立查询条件。
相比于SQL、HQL,QBC的可读性弱。
创建Criteria实例
Criteria接口位于org.hibernate包下,是一个通过组装不同查询条件来获取持久对象的条件类对象。
Session是Criteria实例的工厂。以下是Session创建Criteria的方法:
Criteria createCriteria(Class persistentClass)
通过持久类创建Criteria。查询结果为持久类的集合。
Criteria createCriteria(Class persistentClass, String alias)
通过持久类创建Criteria,并为持久类起一个别名。查询结果为持久类的集合。
Criteria createCriteria(String entityName)
通过实体映射文件的名称创建Criteria。查询结果为持久类的集合。
Criteria createCriteria(String entityName, String alias)
通过实体映射文件的名称创建Criteria,并为持久类起一个别名。查询结果为持久类的集合。
Criterion条件
Criterion接口是Criteria的查询条件。
Criterion接口的主要实现类包括:Example,Junction,SimpleExpression。
Criterion接口中定义两个方法:
TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery)
Return typed values for all parameters in the rendered SQL fragment
String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
Render the SQL fragment
Criterion接口实现类一般通过Restrictions类来创建。
Restrictions的常用方法:
static Criterion allEq(Map propertyNameValues)
Apply an "equals" constraint to each property in the key set of a Map
static LogicalExpression and(Criterion lhs, Criterion rhs)
Return the conjuction of two expressions
static Criterion between(String propertyName, Object lo, Object hi)
Apply a "between" constraint to the named property
static Conjunction conjunction()
Group expressions together in a single conjunction (A and B and C...)
static Disjunction disjunction()
Group expressions together in a single disjunction (A or B or C...)
static SimpleExpression eq(String propertyName, Object value)
Apply an "equal" constraint to the named property
static PropertyExpression eqProperty(String propertyName, String otherPropertyName)
Apply an "equal" constraint to two properties
static SimpleExpression ge(String propertyName, Object value)
Apply a "greater than or equal" constraint to the named property
static PropertyExpression geProperty(String propertyName, String otherPropertyName)
Apply a "greater than or equal" constraint to two properties
static SimpleExpression gt(String propertyName, Object value)
Apply a "greater than" constraint to the named property
static PropertyExpression gtProperty(String propertyName, String otherPropertyName)
Apply a "greater than" constraint to two properties
static Criterion idEq(Object value)
Apply an "equal" constraint to the identifier property
static Criterion ilike(String propertyName, Object value)
A case-insensitive "like", similar to Postgres ilike operator
static Criterion ilike(String propertyName, String value, MatchMode matchMode)
A case-insensitive "like", similar to Postgres ilike operator
static Criterion in(String propertyName, Collection values)
Apply an "in" constraint to the named property
static Criterion in(String propertyName, Object[] values)
Apply an "in" constraint to the named property
static Criterion isEmpty(String propertyName)
Constrain a collection valued property to be empty
static Criterion isNotEmpty(String propertyName)
Constrain a collection valued property to be non-empty
static Criterion isNotNull(String propertyName)
Apply an "is not null" constraint to the named property
static Criterion isNull(String propertyName)
Apply an "is null" constraint to the named property
static SimpleExpression le(String propertyName, Object value)
Apply a "less than or equal" constraint to the named property
static PropertyExpression leProperty(String propertyName, String otherPropertyName)
Apply a "less than or equal" constraint to two properties
static SimpleExpression like(String propertyName, Object value)
Apply a "like" constraint to the named property
static SimpleExpression like(String propertyName, String value, MatchMode matchMode)
Apply a "like" constraint to the named property
static SimpleExpression lt(String propertyName, Object value)
Apply a "less than" constraint to the named property
static PropertyExpression ltProperty(String propertyName, String otherPropertyName)
Apply a "less than" constraint to two properties
static NaturalIdentifier naturalId()
static SimpleExpression ne(String propertyName, Object value)
Apply a "not equal" constraint to the named property
static PropertyExpression neProperty(String propertyName, String otherPropertyName)
Apply a "not equal" constraint to two properties
static Criterion not(Criterion expression)
Return the negation of an expression
static LogicalExpression or(Criterion lhs, Criterion rhs)
Return the disjuction of two expressions
static Criterion sizeEq(String propertyName, int size)
Constrain a collection valued property by size
static Criterion sizeGe(String propertyName, int size)
Constrain a collection valued property by size
static Criterion sizeGt(String propertyName, int size)
Constrain a collection valued property by size
static Criterion sizeLe(String propertyName, int size)
Constrain a collection valued property by size
static Criterion sizeLt(String propertyName, int size)
Constrain a collection valued property by size
static Criterion sizeNe(String propertyName, int size)
Constrain a collection valued property by size
static Criterion sqlRestriction(String sql)
Apply a constraint expressed in SQL.
static Criterion sqlRestriction(String sql, Object[] values, Type[] types)
Apply a constraint expressed in SQL, with the given JDBC parameters.
static Criterion sqlRestriction(String sql, Object value, Type type)
Apply a constraint expressed in SQL, with the given JDBC parameter.
另外,Example类提供一个方法用来创建所有的条件:
static Example create(Object entity)
Create a new instance, which includes all non-null properties by default
protected void addComponentTypedValues(String path, Object component, CompositeType type, List list, Criteria criteria, CriteriaQuery criteriaQuery)
protected void addPropertyTypedValue(Object value, Type type, List list)
protected void appendComponentCondition(String path, Object component, CompositeType type, Criteria criteria, CriteriaQuery criteriaQuery, StringBuffer buf)
protected void appendPropertyCondition(String propertyName, Object propertyValue, Criteria criteria, CriteriaQuery cq, StringBuffer buf)
Example enableLike()
Use the "like" operator for all string-valued properties
Example enableLike(MatchMode matchMode)
Use the "like" operator for all string-valued properties
Example excludeNone()
Don't exclude null or zero-valued properties
Example excludeProperty(String name)
Exclude a particular named property
Example excludeZeroes()
Exclude zero-valued properties
TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery)
Return typed values for all parameters in the rendered SQL fragment
Example ignoreCase()
Ignore case for all string-valued properties
Example setEscapeCharacter(Character escapeCharacter)
Set escape character for "like" clause
Example setPropertySelector(Example.PropertySelector selector)
Set the property selector
String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
Render the SQL fragment
String toString()
Order排序
Order类相当于order by之后的排序字段。
构造方法:
protected Order(String propertyName, boolean ascending)
排序对象。propertyName为排序属性名称。ascending表示是否为正序。
sstatic Order asc(String propertyName)
Ascending order
static Order desc(String propertyName)
Descending order
Order ignoreCase()
String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
Render the SQL fragment
String toString()
Projection条件
Projection是查询条件的另一种表现形式。
Projection主要是提供Criteria的报表查询功能。并能根据条件实现分组。
String[] getAliases()
String[] getColumnAliases(int position)
String[] getColumnAliases(String alias, int position)
Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
Type[] getTypes(String alias, Criteria criteria, CriteriaQuery criteriaQuery)
boolean isGrouped()
String toGroupSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
Projections类是Projection的实力工厂方法。
Projections的方法:
static Projection alias(Projection projection, String alias)
Assign an alias to a projection, by wrapping it
static AggregateProjection avg(String propertyName)
A property average value
static CountProjection count(String propertyName)
A property value count
static CountProjection countDistinct(String propertyName)
A distinct property value count
static Projection distinct(Projection proj)
Create a distinct projection from a projection
static PropertyProjection groupProperty(String propertyName)
A grouping property value
static IdentifierProjection id()
A projected identifier value
static AggregateProjection max(String propertyName)
A property maximum value
static AggregateProjection min(String propertyName)
A property minimum value
static ProjectionList projectionList()
Create a new projection list
static PropertyProjection property(String propertyName)
A projected property value
static Projection rowCount()
The query row count, ie.
static Projection sqlGroupProjection(String sql, String groupBy, String[] columnAliases, Type[] types)
A grouping SQL projection, specifying both select clause and group by clause fragments
static Projection sqlProjection(String sql, String[] columnAliases, Type[] types)
A SQL projection, a typed select clause fragment
static AggregateProjection sum(String propertyName)
A property value sum
添加条件
以上,Criterion和Projection都是对查询条件的描述,在查询前需要把条件交给Criteria接口。相当需为SELECT语句添加WHERE子句。
Criteria接口中定义了如下方法:
Criteria add(Criterion criterion)
该方法为一个Criteria增加一条查询条件。
另外可以使用Property工厂类来定义和持久对象属性的相关条件。
添加排序
若需要对结果排序,在在查询之前把排序字段交给Criteria接口。相当于为SELECT语句添加ORDER BY语句。
Criteria接口中定义了如下方法:
Criteria addOrder(Order order)
该方法为结果集排序。
返回结果
得到查询结果。
Criteria接口中定义了如下方法:
List list()
返回查询结果集。