Struts + Hibernate 之后 ,因为多对一的关系 , 我实体类中的 属性
private int uid ;
private int jdid; 改成了
private TblUser tblUser; private TblJd tblJd;那么相应的在页面下使用Struts 标签 中 property的值 就是 tblUser 了 , 但是页面中的需要填入的值还是 int 类型的 , 将int类型直接装进 tblUser 这类对象中是不可能的 ,会报出
java.lang.IllegalArgumentException: Cannot invoke org.zero.space.entity.TblFwxx.setTblJd - argument type mismatch
这种异常 ,我该怎么写呢?
private int uid ;
private int jdid; 改成了
private TblUser tblUser; private TblJd tblJd;那么相应的在页面下使用Struts 标签 中 property的值 就是 tblUser 了 , 但是页面中的需要填入的值还是 int 类型的 , 将int类型直接装进 tblUser 这类对象中是不可能的 ,会报出
java.lang.IllegalArgumentException: Cannot invoke org.zero.space.entity.TblFwxx.setTblJd - argument type mismatch
这种异常 ,我该怎么写呢?
那就不能用<property>标签了
应该是关系标签<one-to-one>...这种
已经说了是多对一的关系了 但是不能使用<property>标签 ? 我没使用这个标签,我使用的Struts标签,<html:text> 至于关系标签 <one-to-one>... 我在Hibernate中还未学过 这是怎么用的?
非也,外键插入的仍然是id,(见数据库的表字段)
jps页面传到后台的是id,string或Integer或其他类型,Struts 标签无法处理自定义对象类型,
后台比如添加时:
a = session.get(xxx.class, id);//得到对象
b.setxxx(a);
......
save(b);当然效率一点的是在hbm.xml中设many to one 属性insert和update为false,用与查询,
加一项指到同一column,用于加,改,
//将jdid的id弄出来,放进一个刚刚创建的街道对象中,然后把这个对象放入到tblFwxx中的tblJd中去
TblJd tblJd = new TblJd();
tblJd.setJdid(house.getJdid());
tblFwxx.setTblJd(tblJd);
//创建一个当前时间,创建的首先是一个util.date类型的 , 然后通过.getTime()转换成sql.date类型的
java.util.Date d = new java.util.Date();
Date date = new Date(d.getTime());
tblFwxx.setDate(date);
//将session中存储的用户ID取出来,也对象的形式放进tblFwxx中
int userId = ((Integer)session.getAttribute("userId")).intValue();
TblUser tblUser = new TblUser();
tblUser.setUid(userId);
tblFwxx.setTblUser(tblUser);
request.setAttribute("userId", userId);在这里把FormBean中新声明的变量名称中 从jsp页面中获取的街道ID 数据取出 放进一个 在PostAction中特意声明的一个TblJd 对象中 是这种形式,
TblJd tblJd = new TblJd();
tblJd.setJdid(house.getJdid()); 直接把这个特意创建的对象放入到 tblFwxx 中 , 这种形式 tblFwxx.setTblJd(tblJd);这样就把数据全部添加进数据库中了.
呵呵 没有的 我使用Hibernate的POJO 做的 entity层 , 另外有 Struts框架给我生成的formbean所自动生成的form包