Spring+Hibernate3的框架中用到了关联查询
表User:Address是一对多的关系
UserBean中有一个addresses的Set
现在要查询出这样的User,拥有Address中门牌doorplate为"642"的User,
现在DB中只有一个这样的User,这个User拥有门牌doorplate为"642"的Address五个~~
创建DetachedCriteria 的语句如下:
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.
class);
detachedCriteria.createCriteria(
"才").add(Restrictions.like("doorplate","642"));
实际查询语句如下:
List list = getHibernateTemplate().findByCriteria(queryCriteria);
理论上查询出来的list应该User的list是1
总结如下
1:
DetachedCriteria addressCriteria = DetachedCriteria.forClass(User.class).createCriteria("addresses");
addressCriteria.add(Restrictions.eq("doorplate","642")):
2:
DetachedCriteria addressCriteria = DetachedCriteria.forClass(User.class).createAlias("addresses", "a");
addressCriteria.add(Restrictions.eq("a.doorplate","642")):
posted on 2006-10-20 11:10
★yesjoy★ 阅读(5795)
评论(4) 编辑 收藏 所属分类:
Hibernate学习