Reputation: 509
Hi I am solving a family tree problem.
I have a POJO class Family
Public class Family {
private int familyId;
private Partners parents;
private HashSet<Person> children;
private HashSet<Family> descendantFamilies;
//getters and setters.
}
public class Person {
private int personId;
private String name;
private Gender gender; //Enum class
//getters and setters.
}
public class Partners {
private person husband;
private person wife;
//getters and setters
}
now to check if a person has brothers or sisters, I have to check through families where that person is one among the children of that family. I am new to hibernate. I have never searched for a where condition through a list or hashset. Please help.
Upvotes: 2
Views: 1697
Reputation: 3227
Hi I will try to answer your question because I think you're looking for the elements keyword.
You can check through families where that person is one among the children of that family by using below HQL:-
select f
FROM Family f
WHERE (
:person in elements(f.children)
)
this query will give the family containing the person as children. Syntax could be something like:-
String hql = "select f FROM Family f WHERE (:person in elements(f.children))";
Query query = session.createQuery(hql);
query.setEntity("person",somePersonObject);
Also I think you might be already knowing but still want to tell when you are using HashSet overriding equals and hashcode method is important. Like to define two person objects should be considered equal.(possibly when there ids are same)
Upvotes: 2