标签:
你可以使用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();
标签:
原文地址:http://www.cnblogs.com/liuconglin/p/5690117.html