您的位置:程序门 -> java -> j2ee / ejb / jms



spring+hibernate1对1关联中classcastexception


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


spring+hibernate1对1关联中classcastexception
发表于:2007-06-05 15:38:54 楼主
客户表,定单表和定单明细表,客户表和定单表单向1对多,定单表和定单明细表1对1....我在添加定单表的时候怎么老是报这个错java.lang.classcastexception:   java.util.hashset    

customer:  
public   class   cusinfo   {  
private   long   id;  
private   string   cid;  
private   string   cpwd;  
private   string   csex;  
private   string   caddress;  
private   set   order;  
public   string   getcaddress()   {  
return   caddress;  
}  
public   void   setcaddress(string   caddress)   {  
this.caddress   =   caddress;  
}  
public   string   getcid()   {  
return   cid;  
}  
public   void   setcid(string   cid)   {  
this.cid   =   cid;  
}  
public   string   getcpwd()   {  
return   cpwd;  
}  
public   void   setcpwd(string   cpwd)   {  
this.cpwd   =   cpwd;  
}  
public   string   getcsex()   {  
return   csex;  
}  
public   void   setcsex(string   csex)   {  
this.csex   =   csex;  
}  
public   long   getid()   {  
return   id;  
}  
public   void   setid(long   id)   {  
this.id   =   id;  
}  
public   set   getorder()   {  
return   order;  
}  
public   void   setorder(set   order)   {  
order   =   order;  
}  


}  

orderinfo  
public   class   orderinfo   {  
private   long   id;  
private   string   oname;  
private   cusinfo   cus;  
private   set   myorderdetail;  
public   set   getmyorderdetail()   {  
return   myorderdetail;  
}  
public   void   setmyorderdetail(set   myorderdetail)   {  
this.myorderdetail   =   myorderdetail;  
}  
public   cusinfo   getcus()   {  
return   cus;  
}  
public   void   setcus(cusinfo   cus)   {  
this.cus   =   cus;  
}  
public   long   getid()   {  
return   id;  
}  
public   void   setid(long   id)   {  
this.id   =   id;  
}  
public   string   getoname()   {  
return   oname;  
}  
public   void   setoname(string   oname)   {  
this.oname   =   oname;  
}  

}  
orderdetail  
public   class   orderdetail   {  
private   long   id;  
private   double   totalprice;  
private   long   pid;  
private   orderinfo   myorder;  
public   long   getpid()   {  
return   pid;  
}  
public   void   setpid(long   pid)   {  
this.pid   =   pid;  
}  
public   long   getid()   {  
return   id;  
}  
public   void   setid(long   id)   {  
this.id   =   id;  
}  
public   double   gettotalprice()   {  
return   totalprice;  
}  
public   void   settotalprice(double   totalprice)   {  
this.totalprice   =   totalprice;  
}  
public   orderinfo   getmyorder()   {  
return   myorder;  
}  
public   void   setmyorder(orderinfo   myorder)   {  
this.myorder   =   myorder;  
}  

}  

测试用例:  
orderopraimpl   oimpl   =   (orderopraimpl)ctx.getbean( "orderopra ");  
orderinfo   oinfo   =   new   orderinfo();   //创建order实例  
orderdetail   orderd   =   new   orderdetail();//创建orderdetail实例  
orderd.setpid(getproid( "pname ",product));  
orderd.settotalprice(20);  
set   orderds   =   new   hashset();  
orderds.add(orderd);  
//读取客户信息  
cusopraimpl   cimpl   =   (cusopraimpl)ctx.getbean( "cusopra ");  
long   id   =   getcusid(cid,cpwd);  
cusinfo   cus   =   (cusinfo)cimpl.loadcus(id);  
set   orderinfo   =   new   hashset();  
orderinfo.add(oinfo);  
//将定单和客户关联  
cus.setorder(orderinfo);  
//保存order  
oinfo.setcus(cus);  
oinfo.setmyorderdetail(orderds);  
oimpl.addorder(oinfo);  

不知道为什么set   orderinfo   =   new   hashset();会转换失败  
发表于:2007-06-05 15:40:081楼 得分:0
xml:
customer:
<hibernate-mapping>
<class   name= "vo.cusinfo "   table= "customer "   lazy= "false ">
<id   name= "id "   column= "id "   type= "java.lang.long ">
<generator   class= "increment "> </generator>
</id>
<property   name= "cid "   column= "cid "   type= "string "> </property>
<property   name= "cpwd "   column= "cpassword "   type= "string "> </property>
<property   name= "csex "   column= "csex "   type= "string "> </property>
<property   name= "caddress "   column= "caddress "   type= "string "> </property>

<set   name= "order "   cascade= "save-update "   inverse= "true ">
<key   column= "cid "> </key>
<one-to-many   class= "vo.orderinfo "/>
</set>
</class>

order:
<hibernate-mapping>
<class   name= "vo.orderinfo "   table= "orderinfo ">
<id   name= "id "   column= "id "   type= "java.lang.long ">
<generator   class= "increment "> </generator>
</id>
<property   name= "oname "   column= "ordername "   type= "string "> </property>
<many-to-one   name= "cus "   class= "vo.cusinfo "   column= "cid "   lazy= "false ">
</many-to-one>
<map   name= "myorderdetail "   table= "orderdetail "   cascade= "save-update "   inverse= "false ">
<key   column= "id "> </key>
<index   column= "orderid "   type= "java.lang.long "> </index>
<composite-element   class= "vo.orderdetail ">
<parent   name= "myorder "/>
<property   name= "totalprice "   column= "totalprice "   type= "java.lang.double "> </property>
<property   name= "pid "   column= "pid "   type= "java.lang.long "> </property>
</composite-element>
</map>
</class>
发表于:2007-06-08 19:54:362楼 得分:0
最近很忙,没有时间看你的代码不过classcastexception是一个造型异常
肯定是你add到set中的值在取出使用的时候指定的类型不一致


快速检索

最新资讯
热门点击