如果每個美女都有自己的客戶資源(不要想歪了!),那么需要查詢擁有客戶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,所以不會自動生成