Converting a HQL-query which includes elements() to Criteria API
I'm havi开发者_JAVA技巧ng trouble converting the following HQL-query to Criteria API and I was wondering if I could get some help from you guys
SELECT child FROM Foo AS child
WHERE child IN (SELECT elements(obj.foos) FROM Bar AS obj WHERE obj.id = ?)
In other words, I want to fetch all Foos that Bar references to in the instance of Bar whose id is equal to ?.
Edit: Note that I don't have a two-way-relation between the entities, so Foo doesn't know which Bars have a reference to it. Secondly, the reference from Bar to Foo is of type ManyToMany.
These example are not equals to your, but they are similar:
Hibernate Criteria Subquery
Hibernate Criteria With Property Not In (Subquery)
You can use them as example.
Something like:
List<Foo> foos = session.createCriteria(Foo.class).createAlias("bar", "bar").add(Restrictions.eq("bar.id", 12345)).list();
Corresponds to:
class Foo {
Bar bar;
}
class Bar {
long id;
}
精彩评论