Hibernate Criteria.ALIAS_TO_ENTITY_MAP丢失到ArrayList学生 [英] Hibernate Criteria.ALIAS_TO_ENTITY_MAP to ArrayList Student missing

查看:127
本文介绍了Hibernate Criteria.ALIAS_TO_ENTITY_MAP丢失到ArrayList学生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对多的关系,我需要获取渴望的关联表以及使用条件过滤2个表。这是我的代码。

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);
}

一切都很好,但是如果学生没有一部手机可以满足标准限制(isValidPhone = true)学生不在结果集中,即使没有有效的电话,我也仍然需要该学生,因为该应用程序应该能够为该学生添加新的电话,以便有人提出建议。非常感谢

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

推荐答案

这对我有用。.基本上,我将第二个表(关联)表的限制放在general where(查询子句)这意味着1表受到2表限制的影响,这就是窍门。将限制仅放在左外部联接中,这里是一些代码。[您想要在2表中使用的限制。]

this worked for me.. basically i was putting the restrictions of the second table (associate) table on the general where (Query Clause) this means the 1 table is being affect it on this restrictions of the 2 table the trick is here. put the restrictions only in the left outer join here is some code.[the restrictions you want in your 2 table.]

org.hibernate.criterion.Conjunction innerRestrictions = (org.hibernate.criterion.Conjunction)Restrictions.conjunction().add(Restrictions.eq("phoneValid",true)).add(Restrictions.eq("phoneMatch",true));

,然后在此处的2表中检索所有关联。

and then you retrieve all the association in the 2 table here.

like.createCriteria("phone","p",JoinType.LEFT_OUTER_JOIN,innerRestrictions);

在这里,您要求休眠以在现场电话上创建一个内部标准(关系的许多方面)创建别名 p,提取后将其保留在外部联接中,这意味着根本没有电话的学生仍在resultSet上,后来又希望对innerCriteria希望有帮助的条件施加限制。

here you are asking to hibernate to create a innerCriteria on the field phone(the many side of the relationship) creates a alias 'p' the fetch is left outer join means the student without phones at all is still on the resultSet and later the restrictions you want to apply to the criteria the innerCriteria hope it helps.

这篇关于Hibernate Criteria.ALIAS_TO_ENTITY_MAP丢失到ArrayList学生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆