I want to retrieve such object of class A that has "inside" object C.
I have a class A:
public class A {
...
@OneToMany(mappedBy = "classA")
private List<B> listOfB;
}
class B:
public class B {
...
@ManyToOne
@JoinColumn(name = "A_FK")
private A AObject;
@OneToMany(mappedBy = "classB")
private List<C> ListOfC;
}
and class C:
public class C {
...
private String CId;
@ManyToOne
@JoinColumn(name = "B_FK")
private B BObject;
}
Explanation: I have objects A which have list of objects B (one B object can be in only one object A), every object B has inside few objects C (object C can be in only one B).
How can I retrieve object A using single HQL query, knowing Id of object C?
Try this HQL query:
select a from A as a
join a.listOfB as b
join b.listOfC as c
where c.CId = '1234'
Full java example:
Query query = this.session.createQuery("select a from A as a join a.listOfB as b join b.listOfC as c where c.CId = :id");
query.setString("id", "1234");
A a = (A)query.uniqueResult();
It turned out that I don't have to use any joins sin hibernate will join tables by it self. Working query for this example:
select Bclass.AObject
from B as Bclass
where Bclass.id = (select Cclass.Bclass.id FROM C Cclass WHERE c.Id=:Id)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.