随笔 - 6, 文章 - 4, 评论 - 0, 引用 - 0
数据加载中……

DetachedCriteria 用法 (转载)


如果每个美女都有自己的客户资源(不要想歪了!),那么需要查询拥有客户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")):
 
----------------------------------------------------------------------------------------------
 
<prop key="hibernate.hbm2ddl.auto">update</prop>
 
其中update表示加载hibernate自动更新数据库结构,你也可以用create,但这样你数据库中的所有数据都会被清除,估计你现在写的是none,所以不会自动生成 

posted on 2007-12-23 10:02 小石~~ 阅读(1945) 评论(0)  编辑  收藏 所属分类: Hibernate相关


只有注册用户登录后才能发表评论。


网站导航: