Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Query query = session
.createQuery("select a.atdepot_id,i.indepot_drugName,i.indepot_generalName,i.indepot_packageStandard,a.atdepot_quantity,i.indepot_flag,i.indepot_drugStandard,i.indepot_lotNumber,i.indepot_period,i.indepot_unit,i.indepot_productionPlace,i.producer,i.supplier from IndepotDrug i, Atdepot a where i.indepot_drugId=a.indepot_drug");
List<Object> list = query.list();for (int i = 0; i < list.size(); i++) {
Object[] obj = (Object[]) list.get(i);
for (int j = 0; j < obj.length; j++) {
if (obj[j] instanceof IndepotDrug) {
IndepotDrug id = (IndepotDrug) obj[j];
System.out.print(id);


} else if (obj[j] instanceof Atdepot) {
Atdepot ad = (Atdepot) obj[j];
System.out.print(ad);
}
}
System.out.println();
}
这里打印不出对象。上面list.size()测试是有数据的。没有问题
请问这个list<object>怎么打印。怎么显示在jsp上面
我知道用request.setattribute可以传过去。就想知道怎么显示
因为打印不出来,不知道是什么结构

解决方案 »

  1.   

    ....查询出来的object 二个表里面的字段都有,可以考虑再写一个类,这个类就放查询出来字段相对应的属性..
      

  2.   

    hibernate查出来的是个object,可是你没有与之对应的类,如果你一定要这么写,就先写个类,包含所有你需要的属性,一个对一个,
    a.atdepot_id,i.indepot_drugName,i.indepot_generalName,i.indepot_packageStandard,a.atdepot_quantity,i.indepot_flag,i.indepot_drugStandard,i.indepot_lotNumber,i.indepot_period,i.indepot_unit,i.indepot_productionPlace,i.producer,i.supplier
    这多麻烦啊,还不如直接查实体的好,
      

  3.   

    如果只是查询部分字段
    可以用query.scroll()的方法返回ScrollableResults,可以提取出相应字段的值,类似JDBC的ResultSet
    可以查看API相关类如果是要对象的话用2楼的方法
      

  4.   

    直接写sql语句得了不用hql  createSQLQurey
    return hibernateSession.createSQLQuery("select * from EmolumentLeafletInfo emo where emo.payment_Id="+id).addEntity(EmolumentLeafletInfo.class).list();
    给你个例句自己套
      

  5.   

    List<Object> list = query.list();这里用Object大了点吧。应该是个list<String> 类型的吧。
    这样你就可以通过list.get(0).get(0).来遍历了。
    封装到一个对象是hibernate,或者说是MVC的普遍做法。但是这个对象并不对应数据库中的表所以无法进行持久化管理,可以直接封装到一个map里面,这样就不用多建一个类了。
      

  6.   

    我自己建立了个form表单。现在的问题是不知道怎么把数据set进去obj[0]这个数据里面的东西怎么拿出来 这一个对应了一条数据哦
      

  7.   

     List<Object[]> list = query.list();
    到这里是对的。下面的都是错的。你们根本就没说重点。没人会就无满意结帖了
    是一个数组。我上面写少了点
      

  8.   

    前面说错了 应该是List<List<String>> list = query.list(); 
    然后通过list.get(i).get(i)来遍历。不是没人说到重点。别人让你封装一个类处理也是对的,只是你自己不知道!!
      

  9.   

    欢迎各位java大侠、小虾加入QQ高级群:45271133  群满500人为止,热烈邀请大侠们加入指点~!
      

  10.   

    谢谢,学习了,hql可直接写成这样select a,i from IndepotDrug i, Atdepot a where i.indepot_drugId=a.indepot_drug