码迷,mamicode.com
首页 > 其他好文 > 详细

createCriteria()的理解

时间:2016-07-21 00:33:05      阅读:5890      评论:0      收藏:0      [点我收藏+]

标签:

你可以使用createCriteria()非常容易的在互相关联的实体间建立 约束。 

官网上给出的代码案例
List cats = sess.createCriteria(Cat.class) 
.add( Restrictions.like("name", "F%") 
.createCriteria("kittens") 
  .add( Restrictions.like("name", "F%") 
.list(); 

 

下面的例子更有利于理解createCriteria()的含义:

比如
这样几个持久化对象
public class Com{
private String name;
private Set<Department> departments;
//get set 省略
}

public class Department{
private String name;
private Com com;
//get set 省略
}

public class ComPersonnel{
private String name;
private Department department;
//get set 省略
}

HQL 语句:

from
ComPersonnel comPersonnel
join comPersonnel.department
join comPersonnel.department.com
where
comPersonnel.department.com.name = ‘大海公司‘

等价与

Session session = //得到org.hibernate.Session
Criteria criteria = session.createCriteria(ComPersonnel.class);
Criteria Criteria4Department = criteria.createCriteria("department");
Criteria Criteria4Com = Criteria4Department.createCriteria("com");
Criteria4Com.add(Restrictions.eq("name", "大海公司"));
criteria.list();

 

createCriteria()的理解

标签:

原文地址:http://www.cnblogs.com/liuconglin/p/5690117.html

(3)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!