public AllWmsInfo findAllWmsInfo(Integer start,Integer end){
tr=getTr();
Query query=session.createQuery("select p.productName,w from Product p,WmsInfo w where p.productId=w.productId");
List li=query.list();
System.out.println(li.size());
Iterator it=li.iterator();
while(it.hasNext()){
Object[] a=(Object[])it.next();
AllWmsInfo pp=(AllWmsInfo) a[0];
System.out.println(pp.getProductName());
}
return null;
}
我要得到的AllWmsInfo对象
一直报错java.lang.String cannot be cast to wk.len.domain.AllWmsInfo
能够有什么办法等到AllWmsInfo对象 要怎么样修改
我AllWmsInfo 中包含productName 和WmsInfo中的 所有字段
tr=getTr();
Query query=session.createQuery("select p.productName,w from Product p,WmsInfo w where p.productId=w.productId");
List li=query.list();
System.out.println(li.size());
Iterator it=li.iterator();
while(it.hasNext()){
Object[] a=(Object[])it.next();
AllWmsInfo pp=(AllWmsInfo) a[0];
System.out.println(pp.getProductName());
}
return null;
}
我要得到的AllWmsInfo对象
一直报错java.lang.String cannot be cast to wk.len.domain.AllWmsInfo
能够有什么办法等到AllWmsInfo对象 要怎么样修改
我AllWmsInfo 中包含productName 和WmsInfo中的 所有字段
改为
select p.productName,w.* from Product p,WmsInfo w where p.productId=w.productId
select w.* from Product p,WmsInfo w where p.productId=w.productId
a[0] 是
Query query=session.createQuery("select p.productName,w from Product p,WmsInfo w where p.productId=w.productId");
查询出来的部分,是一个String,强制转换成AllWmsInfo当然要出错了!
不知道仁兄是否对AllWmsInfo进行了持久化,看样子应该是没有
String hql ="select p.productName,w from Product p,Wms w where p.productId = w.productId";
List list = session.createQuery(hql).list();
System.out.println(list.size());
Iterator it = list.iterator();
while(it.hasNext()){
Object[] a = (Object[])it.next();
AllWmsInfo info = new AllWmsInfo();
info.setProductName((String)a[0]);
info.setW((Wms)a[1]);
System.out.println(info.getProductName()+"----"+info.getW().getProductId());
}
应该可以解决之。
其实是可以一个一个set进去 但我觉得肯定有其他方法的 不然如果属性很多 那代码多恐怖
expecting IDENT, found '*' near line 1, column 24 [select p.productName,w.* from wk.len.po.Product p,wk.len.po.WmsInfo w where p.productId=w.productId]
AllWmsInfo既然是2个类所有属性的写在一个类,你可以用hibernate的方法
public List getListObject(final String sql, final Map para, final String type, final Class cls) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException { Query query = session.createSQLQuery(sql);
List list = query.list();
return list; }
}, true);
}
Product 和 WmsInfo
通过这id值相同的条件查询 所有的WmsInfo 类吧?
p.productId = w.productId如果没理解错的话 试试用transformer一种使用sqlsql 就按照你查询数据库方式查询 不过要说的是、你WmsInfo表 里面的字段查出来要 as 给你AllWmsInfo 实体类里面的属性 (如果数据库表字段和类属性字段完全一样就不用as 了)Query sqlQuery = session.createSQLQuery(“你的sql语句”).setResultTransformer(new AliasToBeanResultTransformer(AllWmsInfo.class));二种使用hqlpublic List getAllList() {
String hql =" from Product as p , WmsInfo as w where p.productId=w.productId";
List list = session.createQuery(hql).list();
List<AllWmsInfo> reList = new ArrayList<AllWmsInfo>();
for(Object obj:list){
Object[] o = (Object[])obj;
AllWmsInfo all = (AllWmsInfo)o[0];
reList.add(all);
}
return reList;
}
2、public AllWmsInfo findAllWmsInfo(Integer start,Integer end){
tr=getTr();
Query query=session.createQuery("select p.productName,w from Product p,WmsInfo w where p.productId=w.productId");
List li=query.list();System.out.println(li.size());
Iterator it=li.iterator();
while(it.hasNext()){
Object[] a=(Object[])it.next();
AllWmsInfo pp=(AllWmsInfo) a[0];
System.out.println(pp.getProductName());
}
return null;
}
我要得到的AllWmsInfo对象
你的代码中,hql对与错我暂且不说,但是Object[] a=(Object[])it.next();这样你自己都写的是得到的object数组,a[0]又怎么可能是AllWmsInfo类型呢?
你要循环,将a[0],a[1],a[2],a[3],a[4],a[5]等等分别拿出来,封装进入AllWmsInfo。