日期:2014-05-19  浏览次数:20679 次

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());
}
应该可以解决之。
------解决方案--------------------
探讨
我这个AllWmsInfo是自己创建的一个类 数据库里面没有这个表 但我想到的就是这东西

------解决方案--------------------
探讨

我这个AllWmsInfo是自己创建的一个类 数据库里面没有这个表 但我想到的就是这东西

------解决方案--------------------
探讨
为什么如果我吧w该为w.*的时候 会报错
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]

------解决方案--------------------
1、你对hibernate不熟悉
2、
Java code

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对象