HQL 联合查询问题 求教
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中的 所有字段
[解决办法]
select p.productName,w from Product p,WmsInfo w where p.productId=w.productId
改为
select p.productName,w.* from Product p,WmsInfo w where p.productId=w.productId
[解决办法]
select * from WmsInfo w where w.productId in(select productId from Product)
[解决办法]
select p.productName,w from Product p,WmsInfo w where p.productId=w.productId
这个得到是两个东西,第一个是productName ,第二个才是WmsInfo,你将第一个强制转换为WmsInfo肯定回报异常的。
改为:
select w.* from Product p,WmsInfo w where p.productId=w.productId
[解决办法]
额,应该是While循环里AllWmsInfo pp=(AllWmsInfo) a[0];出的错吧?
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());
}
应该可以解决之。
[解决办法]
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对象