Criteriacriteria = this.getSession().createCriteria(EquipmentPO.class);
criteria. add (Restrictions.allEq(newMap(Restrictions.eq("equipID",newString(" 11020449 ")),Restrictions.eq("equipID",newString(" 11020449 "))));
// 注意:between查询条件可解释为查询EquipmentPO对象中的equipID属性值在newString(" 11020449 ")和newString(" 11030137 ")之间的所有记录值(包含两个端点)
criteria. add (Restrictions. between ("equipID",newString(" 11020449 "),newString(" 11030137 ")));
// 查询结果列表记录按照equipID的属性值来升序排序
criteria.addOrder( Order . asc ("equipID")); // 降序方式为: Order . desc ("equipID")
// 通过EquipmentPO对象的主键id来查询
criteria. add (Restrictions.idEq("402882ac0d3f7ca8010d3f7ef869000b"));
Restrictions. like (fieldName," % " + para + " % ")
// 使用ilike方式进行模糊查询
criteria. add (Restrictions.ilike("equipID",newString(" % 11020 % ")));
// ilike的i即ignore之意,所以这里查询出englishName值为"OptimaXL100KUltracentrifuge"(忽略大小写)的记录
criteria. add (Restrictions.ilike("englishName",newString("OptimaXL100KUltracentrifuge"),MatchMode.ANYWHERE)); // 这里
// 使用in方式有两种形式,即数组或者Collection的方式,可参考如下两个实例
criteria. add (Restrictions. in ("equipID",newString [] {" 11020449 "," 11020450 "})); // 数组参数
Collectioncol = newArrayList();
col. add (newString(" 11020449 "));
col. add (newString(" 11020450 "));
col. add (newString(" 11020874 "));
criteria. add (Restrictions. in ("equipID",col)); // Collection参数
// 使用isEmpty / isNotEmpty方式用来判断EquipmentPO对象中的Collection类型的属性是否为空的所有记录
// EquipmentPO对象中定义属性private Set equipFunctionDevelopPOs = newHashSet(); // 设备功能开发对象
criteria. add (Restrictions.isEmpty("equipFunctionDevelopPOs"));
criteria. add (Restrictions.isNotEmpty("equipFunctionDevelopPOs"));
// 使用isNull方式查询出所有schoolID属性没有值的记录。说明:Restrictions.isNull判断属性是否为空,为空返回true,反之返回false
criteria. add (Restrictions. isNull ("schoolID"));
criteria. add (Restrictions.isNotNull("schoolID"));
// 与Restrictions.eq正好相反,表示不存在( not in )
criteria. add (Restrictions. not (Restrictions.eq("equipID",newString(" 11020449 "))));
// 使用Restrictions.sizeEq方式用来查询EquipmentPO对象中的Collection类型的属性equipFunctionDevelopPOs的size为1的所有记录
criteria. add (Restrictions.sizeEq("equipFunctionDevelopPOs", 1 ));
// 使用sql限定的查询。注意{alias}.chnname这里是指实际表中的字段名而非属性名(不区分大小写)
criteria. add (Restrictions.sqlRestriction("{alias}.chnname like (?)"," % 电 % ",Hibernate.STRING));
criteria. add (Restrictions.sqlRestriction("{alias}.ENGNAME like (?)"," % ptima % ",Hibernate.STRING));
// 如果有多个查询条件,比如between子句的查询则如下:
BigDecimal [] unitPrice = {newBigDecimal( 402514 ),newBigDecimal( 614891 )};
Type [] types = {Hibernate.BIG_DECIMAL,Hibernate.BIG_DECIMAL};
criteria. add (Restrictions.sqlRestriction("{alias}.unit_price between (?) and (?)",unitPrice,types));
Listlist = criteria.list();
System.out.println("size ===> " + list.size());
return list;
原文地址:http://www.blogjava.net/ocean07000814/articles/72601.html