DetachedCriteria 详细使用

DetachedCriteria 详细使用

一、基本使用

. . . 1. 说明
Restrictions 是产生查询条件的工具类。
. . . 2. 定义

可以直接用 class 创建

DetachedCriteria searDc =
DetachedCriteria.forClass(QymlPerson.class);


也可以用 hibernate 的 session 创建
session.createCriteria(Student.class)
. . . 3. 条件查询
3.1 多条件的 and 规则
通过 searDc.add(Restrictions.eq("unid", userid))实现条件查询。
多次添加的条件,默认的规则是 and.
3.2 多条件的 or 规则
如果实现 or 的查询,需要按照如下方式进行
searDc.add(Restrictions.or(Restrictions.eq("deptunid", "aa"),
Restrictions.isNull("deptunid")));


其中 isnull 表示一个常规字段是否为空,isEmpty 用来表示一个集合字段是否为空。
. . . 4. 查询排序

通过

 searDc.addOrder(Order.asc(propertyName1))


可以添加排序,如果有多个排

序字段,可以添加多次;最终的结果将按照添加的次序进行排序处理。


二、子查询
//主查询:人员查询
DetachedCriteria searDc =
DetachedCriteria.forClass(QymlPerson.class);


//子查询:职务人员关系表
DetachedCriteria sub =
DetachedCriteria.forClass(QymlPositionUserLink.class);
sub.add(Restrictions.eq("positionunid", positionunid));


//子查询:指定查询的列(也就是select usernuid from ....)
sub.setProjection(Property.forName("userunid"));


//主查询和子查询关联(也就是 where unid in (select userunid from...) )
searDc.add(Property.forName("unid").in(sub));


在上面的例子中,用个一个类似于下面 SQL 的子查询
Select * from Person a where a.unid in (select userunid from PositionUserLink b where
b.positionunid = ..)

Property 还有其他的条件判断,参考 api
http://docs.jboss.org/hibernate/core/3.3/api/org/hibernate/criterion/Property.html。


三、Restrictions 表达式


HQL 运算符QBC 运算符含义
==Restrictions.eq()等于 equal
<>Restrictions.ne()不等于 not equal
>>Restrictions.gt()大于 greater than
>=Restrictions.ge()大于等于 greater than or equal
<<Restrictions.lt()小于 less than
<=Restrictions.le()小 于 等 于 less than or equal
is nullRestrictions.isnull()等于空值
is not nullRestrictions.isNotNull()非空值
likeRestrictions.like()字符串模式匹配
andRestrictions.and()逻辑与
andRestrictions.conjunction()逻辑与
orRestrictions.or()逻辑或
orRestrictions.disjunction()逻辑或
notRestrictions.not()逻辑非
in(列表))Restrictions.in()等于列表中的某一个值
not in(列表))Restrictions.not(Restrictions.in())不等于列表中任意一个值
between xx and yyRestrictions.between()闭区间 xy 中的任意值
not between xx and yyRestrictions.not(Restrictions..between())小于值 X 或者大于值 y




四、参考资料

1、浮尘俗世的博客,“HQL 语句的语法”系列笔记
http://blog.163.com/xiaokangzhijia@126/blog/static/1659548562010112692011846/
2、hibernate 的 api
http://docs.jboss.org/hibernate/core/3.3/api/org/hibernate/criterion/
3、没有昵称的 BLOG

http://blog.sina.com.cn/s/blog_4a04ce760100hyzv.html


		DetachedCriteria criteria = DetachedCriteria.forClass(UserProfile.class);
		criteria.createAlias("user","user");
//		criteria.add(Restrictions.eq("userType", UserTypeEnum.PROVIDER));
		DetachedCriteria sub = DetachedCriteria.forClass(Agent.class);
		sub.add(Restrictions.eq("agentProviderType", AgentProviderRoleEnum.AGENTER));
		sub.createAlias("saler","saler");
		sub.createAlias("saler.user","salerUser");
		sub.setProjection(Property.forName("salerUser.id"));  
		criteria.add(Restrictions.or(Restrictions.eq("userType", UserTypeEnum.PROVIDER), Property.forName("user.id").in(sub)));
        
		if(query != null){
			if(query.getUser() != null){
				criteria.add(Restrictions.ilike("user.realName", query.getUser().getRealName(), MatchMode.ANYWHERE));
			}
		}


关于DetachedCriteria的group by和使用公式

Project 主要是让 Criteria 能够进行报表查询,并可以实现分组。 Project 主要有
SimpleProjection 、 ProjectionList 和 Property 三个实现。其中 SimpleProjection 和
ProjectionList 的实例化是由内建的 Projections 来完成,如提供的 avg 、 count 、 max 、
min 、 sum 可以让开发者很容易对某个字段进行统计查询。
       Property 是对某个字段进行查询条件的设置,如通过Porperty.forName(“color”).in
(new String[]{“black”,”red”,”write”}); 则可以创建一个 Project 实例。通过
criteria 的 add(Project) 方法加入到查询条件中去。
    使用 Criteria 进行查询,主要要清晰的是 Hibernate 提供了那些类和方法来满足开发中查
询条件的创建和组装,下面介绍用法:

	
ProjectionList projectionList = Projections.projectionList(); 
	projectionList.add(Projections.groupProperty("agentApplyStatus")); 
	projectionList.add(Projections.count("agentApplyStatus"));	
	DetachedCriteria criteria = DetachedCriteria.forClass(UserProfile.class);
	criteria .setProjection(projectionList);



ProjectionList projectionList = Projections.projectionList(); 
	projectionList.add(Projections.groupProperty("agentApplyStatus")); 
	projectionList.add(Projections.count("agentApplyStatus"));	
	DetachedCriteria criteria = DetachedCriteria.forClass(UserProfile.class);
	criteria .setProjection(projectionList);





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值