views:

261

answers:

1

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.

A: 

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
Like I said, Foo does NOT have a reference to Bar, it's only Bar that has references to to Foo.
Kim L