detached entity passed to persist: 问题的解决

在结构中使用了OneToOne的表单映射。
但是在保存主,子表的时候,发生 detached entity passed to persist: 的错误。
解决办法:
去掉主表中     
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.GeneratedValue;
@GenericGenerator(name = "generator", strategy = "uuid")
@GeneratedValue(generator = "generator")
这个注释。

原意是使用 hibernate uuid 来进行自动id的赋值。但是hibernate 3.3版本好像有问题。无法完成自动功能。

只能使用如下代码来进行id复制。
  1.   public boolean addUserInfo(AlcorTUser alcorUser) throws Exception{
  2.         String userID = UUID.randomUUID().toString();
  3.         alcorUser.setUserid(userID);
  4.         alcorUser.setPassword("password");
  5.         alcorUser.setLoginCount(0);
  6.         alcorUser.setLastLogin(new Date());
  7.         alcorUser.getAlcorTUserDetail().setUserid(userID);
  8.         /*Iterator<AlcorTUserDetail> userDetailIterator =  alcorUser.getAlcorTUserDetails().iterator();
  9.         logger.debug("含有"+alcorUser.getAlcorTUserDetails().toArray().length+"个用户详细信息");
  10.         while (userDetailIterator.hasNext())
  11.         {
  12.             userDetailIterator.next().setUserid(userID);
  13.             //this.alcorTUserDetailDAO.save(userDetailIterator.next()) ;
  14.         }*/
  15.         this.alcorTUserDAO.save(alcorUser);
  16.        
  17.         return true;
  18.     }
其中 alcorUser 是主表, alcorUserDetail是子表,是OneToOne的映射。
传入的alcorUser对象是flex通过lcds传入。已经在flex这端。把一个子表内容的对象放入到这个主表内容的对象中去了。所以在这里只要对主表进行save,自动的会对子表也进行保存。

中间屏蔽的这端是处理OneToMany的方法。使用了Iterator。在POJO这端通过HashSet来定义。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值