hibernate子查询

以前以为复杂的子查询只能用hql实现,今天高人指点,原来也可以用Criteria对象实现。

例如,查询a表中没有在b表出现的记录
SQL: select * from tableA a where not exists (select 1 from tableB b where b.id=a.id and b.name=**)

hibernate:
DetachedCriteria dCriteria = DetachedCriteria.forClass(TableB.class, "b").setProjection(Property.forName("id")).add(Expression.and(Restrictions.eqProperty("a.id", "b.id"), Expression.eq("b.name", name)));
Session.CreateCriteria(TableA.class,"a").add(Subqueries.notExists(dCriteria)).List();

你可能感兴趣的:(Hibernate)