hiberaten 多表查询结果处理

 

hiberaten 多表查询结果处理:
在以前的项目里,经常会遇到关于hiberaten多表联系查询的处理,
在网上看到不少的同行问到关于hiberate 多表查询的结果如何处理,以下是我个人的一些项目经验,分享给大家看一下,
如果有什么好的建议的话,请提出:
/**
 *
 * @author jbeduhai
 */

public class MessageServiceImp extends BusinessClass implements MessageService

//处理方法一:
public void getMessage(){
 //取得收件箱内容,传一个消息类型,一个HQL语句;
       List list =  this.getMessageByHql("",
               "  select m.title,m.body,m.event_time,m.sender  "+
               "  from MessageImp m,MessageReceiversImp r  "+
               "  where m.id = r.messageid  and  m.type =1  and r.receiverid='userid' and r.tag =0");

 //MessageI 一方,MessageReceiversI 多方,嵌套输出
        for (Iterator ite = list.iterator(); ite.hasNext();) { 
    
            MessageI messageI = (MessageI) ite.next();  //实体类接口
            System.out.println(">>>>>>>>>>>>>>getTitle>>>>>>>>>>>>>" + messageI.getTitle());
     System.out.println(">>>>>>>>>>>>>>getBody>>>>>>>>>>>>>" + messageI.getBody());
            System.out.println(">>>>>>>>>>>>>>getSender>>>>>>>>>>>>>" + messageI.getSender());
            System.out.println(">>>>>>>>>>>>>>getEvent_time>>>>>>>>>>>>>" + messageI.getEvent_time());
            System.out.println("=================== set=" + messageI.getMessageReceiversImpSet().size());
            for (Iterator ite1 = messageI.getMessageReceiversImpSet().iterator(); ite1.hasNext();) {
                MessageReceiversI messageReceiversI = (MessageReceiversI) ite1.next();
                System.out.println("=================== getId=" + messageReceiversI.getId());
                System.out.println("=================== getReceiverid=" + messageReceiversI.getReceiverid());
                System.out.println("=================== getReplybody=" + messageReceiversI.getReplybody());
            }
        }
}


//处理方法二:
public void getMessage(){
 List list = this.getMessageByHql(
                "  from MessageImp m,MessageReceiversImp r  " +
                "  where m.id = r.messageid  and  m.type =1  and r.receiverid='user9' and r.tag =0");
        Iterator ite = list.iterator();

 //MessageI 一方,MessageReceiversI 多方
        while (ite.hasNext()) {
            Object[] o = (Object[]) ite.next();

     //索引值是根据查询时对象的多少来定义的,从0开始,0到 n-1个索引
            MessageI messageI = (MessageI) o[0];
            MessageReceiversI messageReceiversI = (MessageReceiversI) o[1];
           
            System.out.println("=================== getTitle=" + messageI.getTitle());
            System.out.println("=================== getBody=" + messageI.getBody());

            System.out.println("=================== getId=" + messageReceiversI.getId());
            System.out.println("=================== getSenderid=" + messageReceiversI.getSenderid());
            System.out.println("=================== getReceiverid=" + messageReceiversI.getReceiverid());
            System.out.println("=================== getReplybody=" + messageReceiversI.getReplybody());
        }
}

 

    /**
     *
     * @return hql 查询HQL语句
     */

    public List getMessageByHql(String hql) {
        List list = new ArrayList();
        try {

     //根据spring 配置得到 工厂类实现对象
            MessageFactoryI messageFactoryImp =
            (MessageFactoryImp) factory.getBean("messageFactoryImp");

     //根据工厂类  得到Dao实现类对象
            MessageDB messageDB = messageFactoryImp.getMessage();
            list = this.getMessageByHql(hql);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }


      /**
     *
     * @param hql
     * @return 根据hql返回消息表中符合条件的消息
     *
     */
    public List<MessageI> getMessageByHql(String hql) {
        Session session = HibernateSessionFactory.currentSession();
        Query query = session.createQuery(hql);
        return query.list();
       
    }

  }


  //由于时间关系,贴出不是很全,全面的整理后,再贴出,以上希望对需要的朋友有帮助;

你可能感兴趣的:(DAO,spring)