我有两张桌子:
table
(id, name, title, ...)
(master_id, attribute_name, attribute_value)
#2的示例数据:
- 10, "authors", "John Bill"
- 10, "authors", "Merry J"
- 10, "owners", "Chris O."
- 11, "authors", "Andrew K."
这是一对多的关系:
<set name="repeating" table="xxx" cascade="none" mutable="false" lazy="true" fetch="join">
<key column="...."/>
<one-to-many class="...." />
</set>
我想找到主对象(id=10)
,其中"authors" = "John Bill"
和"authors" = "Merry J"
以及"owners" = "Chris O."
对于第一个条件,我可以这样做:
session.createCriteria(Master.class)
.createCriteria("repeating")
.add(Restrictions.eq("attributeName", "authors"))
.add(Restrictions.eq("attributeValue", "John Bill"))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
如何使用Criteria添加其他条件?
由于
答案 0 :(得分:0)
我想你想要的是分离标准
DetachedCriteria dc = DetachedCriteria.forEntityName("repeating")
.add(Restrictions.eq("attributeName", "authors"))
.add(Restrictions.eq("attributeValue", "John Bill"))
// other rows
.setProjection(Projections.distinct(Property.forName("masterId")));
session.createCriteria(Master.class)
.add(Property.forName("id").in(dc))
.list();