I have one to many relationship I need to fetch eager the associate table as well filters the 2 tables using criteria. Here is my code..
public ArrayList<Student>getListOfStudents() {
Session session = getHibernateTemplate().getSessionFactory().openSession();
Criteria like = session.createCriteria(Student.class)
.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
prepareForSelect(like);//some filtering
Criteria innerCriteria = like.createCriteria("phone","p",JoinType.LEFT_OUTER_JOIN);
prepareForSelect(innerCriteria);//same filtering but in phone now..
like.addOrder(Order.asc("c01"));
Iterator<java.util.Map<Object,Object>> ite = like.list().iterator();
HashSet<Student>students= new HashSet<Student>();
while(ite.hasNext())
{
java.util.Map<Object,Object> map = ite.next();
Student aStudent = (Student)map.get(Criteria.ROOT_ALIAS);
Phone phone = (Phone)map.get("p");
if(student.contains(aStudent))
students.addPhoneToStudent(phone); //add to phone hashSet in Student.class
else
{
student.setListOfPhones(new HashSet<Phone>(0));
students.addPhoneToStudent(phone);
students.add(student);
}
session.close();
return new ArrayList<Student>(students);
}
everything is OK but if a Student dont have a phone which fullfill the criteria restrictions(isValidPhone=true) the Student is not in the resultset i need still the student even if not have a valid phone because the App should be able to add a new phone for the student can somebody give a tip. thank a lot