当前位置: 代码迷 >> 综合 >> Hibernate Criteria 笔记
  详细解决方案

Hibernate Criteria 笔记

热度:65   发布时间:2023-12-08 17:43:24.0

MatchMode的四种匹配模式

ANYWHERE

Match the pattern anywhere in the string

匹配字符串的任意部分,转换成sql为 columnName like '%value%'

END

Match the end of the string to the pattern

匹配字符串的结束部分,转换成sql为columnName like %value

EXACT

Match the entire string to the pattern

匹配整个字符串,转换成sql为columnName like value

START

Match the start of the string to the pattern

匹配字符串的开始部分,转换成sql为columnName like value%

Restrictions 设置具体的条件

常用的静态方法
eq(String propertyName, Object value)
转换成sql为propertyName=value

in(String propertyName, Object[] values)
转换成sql为propertyName in (values[0],values[1],....,values[len-1])

isNotNull(String propertyName)
转换成sql为propertyName is not null

isNull(String propertyName)
转换成sql为propertyName is null

like(String propertyName, Object value)
转换成sql为propertyName like value

le(String propertyName, Object value)
转换成sql为propertyName <= value

lt(String propertyName, Object value)
转换成sql为propertyName < value

ge(String propertyName, Object value)
转换成sql为propertyName >= value

gt(String propertyName, Object value)
转换成sql为propertyName > value

ne(String propertyName, Object value)
转换成sql为propertyName != value

sqlRestriction(String sql,Object value,Type type)
eg:
Restrictions.sqlRestriction(“lower({alias}.name) like lower(?)”, “Fritz%”, Hibernate.STRING)
{alias}在运行的时候会被替换成表的别名,不需要手动调整

Conjunction 、Disjunction

Conjunction
将多个过滤条件以逻辑与的格式拼接在一起
Disjunction
将多个过滤条件以逻辑或的格式拼接在一起

由于ConjunctionDisjunction的构造函数是被protected和缺省值修饰的,所以无法在org.hibernate.criterion包的外部进行调用

创建Conjunction、Disjunction对象

Conjunction conjunction = Restrictions.conjunction();
Disjunction disjunction = Restrictions.disjunction();

Projections 处理结果

List results = session.createCriteria(Cat.class).setProjection( Projections.projectionList().add( Projections.rowCount(), "catCountByColor" ).add( Projections.avg("weight"), "avgWeight" ).add( Projections.max("weight"), "maxWeight" ).add( Projections.groupProperty("color"), "color" )).addOrder( Order.desc("catCountByColor") ).addOrder( Order.desc("avgWeight") ).list();
List results = session.createCriteria(Cat.class).setProjection( Projections.projectionList().add( Projections.rowCount().as("catCountByColor") ).add( Property.forName("weight").avg().as("avgWeight") ).add( Property.forName("weight").max().as("maxWeight") ).add( Property.forName("color").group().as("color" )).addOrder( Order.desc("catCountByColor") ).addOrder( Order.desc("avgWeight") ).list();

Criteria

创建Criteria对象

Criteria criteria = session.createCriteria()

add(Criterion criterion) 添加过滤条件
list() 得到结果
addOrder(Order order) 为结果添加排序

Order 设置排序规则
升序
public static Order asc(String propertyName)
降序
public static Order desc(String propertyName)

demo

List cats = sess.createCriteria(Cat.class).add( Restrictions.like("name", "F%") ).addOrder( Order.asc("name") ).addOrder( Order.desc("age") ).setMaxResults(50).list();
List cats = sess.createCriteria(Cat.class).add( Property.forName("name").like("F%") ).addOrder( Property.forName("name").asc() ).addOrder( Property.forName("age").desc() ).setMaxResults(50).list();

以上两个demo是Hibernate官网提供的,同样的效果不同的写法

这里写图片描述

  相关解决方案