首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Hibernate Criteria 联系关系查询

2012-11-21 
Hibernate Criteria 关联查询http://www.blogjava.net/hilor/archive/2007/09/14/145172.html?前面讲了Cri

Hibernate Criteria 关联查询

http://www.blogjava.net/hilor/archive/2007/09/14/145172.html

?

前面讲了Criteria看起来比HQL顺眼多了,接着继续。

如果每个美女都有自己的客户资源(不要想歪了!),那么需要查询拥有客户Gates的美女怎么办?

使用Criteria可以有两种方法:

1:
DetachedCriteria beautyCriteria = DetachedCriteria.forClass(Beauty.class).createCriteria("customers");
beautyCriteria.add(Restrictions.eq("name", "Gates")):

2:
DetachedCriteria beautyCriteria = DetachedCriteria.forClass(Beauty.class).createAlias("customers", "c");
beautyCriteria.add(Restrictions.eq("c.name", "Gates")):

接着有了新的要求,年纪太大的美女不要,还是查找拥有客户Gates的,条件如下:
DetachedCriteria beautyCriteria = DetachedCriteria.forClass(Beauty.class, "b").;
DetachedCriteria customerCriteria = beautyCriteria.createAlias("customers", c");
beautyCriteria.add(Restrictions.le("b.age", new Long(20))):
customerCriteria.add(Restrictions.eq("c.name", "Gates")):

关于Criteria更详细的资料,Hibernate的源代码和测试是最好的文档。

Criteria的缺点?DBA很生气,后果很严重。

热点排行