如何使用Subqueries.in

时间:2014-06-24 13:06:49

标签: java hibernate hibernate-criteria

如何通过条件API表达此类HQL查询?

select a from A a where
(select b.prop from B b where b.id = a.parent) in ('1', '2', '3')

ID列表作为参数传递。

这是我的子查询:

DetachedCriteria subQuery = DetachedCriteria.forClass(B.class, "b").
                .add(Restrictions.eqProperty("b.id", "a.parent"))
                .setProjection(Projections.property("b.prop"));

Subqueries.in propertyIn 以相反的方式工作(prop在子查询中,而不是我需要的prop中的子查询)。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用Restrictions.in(...)

List<Integer> values=new ArrayList<Integer>();
values.add(1);
values.add(2);
values.add(3);

DetachedCriteria subQuery = DetachedCriteria.forClass(B.class, "b").
            .add(Restrictions.eqProperty("b.id", "a.parent"))
            .setProjection(Projections.property("b.prop")).
            .add(Restrictions.in("b.prop", values);