0

I'm having 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.

KCL
  • 6,733
  • 10
  • 37
  • 43

2 Answers2

0

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.

Community
  • 1
  • 1
Giorgio Desideri
  • 169
  • 1
  • 12
-2

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;
}
Droo
  • 3,177
  • 4
  • 22
  • 26
  • Like I said, Foo does NOT have a reference to Bar, it's only Bar that has references to to Foo. – KCL Jan 26 '10 at 18:55