Wednesday, May 23, 2012

Hibernate HQL Select Query Based for elements in a One-to-Many Relationship

Suppose you have a two classes as mentioned below.

public class Parent{
       private String name;
       private String company;
       private Set<Child> children;
}

public class Child{
       private String name;
       private String school;
}

Writing select query for the equivalent SQL tables from native SQL is a very simple thing while writing HQL can be a bit tricky for a one who is new to HQL.
You can write the select HQL query for above two classes as mentioned below.

Ex: Requirement find the parent details of the children named "Amda"

select parent from Parent parent left join parent.children childrenList where childrenList.name = "Amda"