我有一对多的关系,我需要急切地获取关联表,并使用条件过滤这两个表。这是我的代码..
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);
}
一切正常,但如果学生没有满足条件限制的电话(isValidPhone=true),学生不在结果集中,即使没有有效电话,我仍然需要学生,因为应用程序应该能够添加新的学生的电话可以有人给小费。非常感谢