caonima008
caonima008
2012-10-18 17:18

hibernate 一对一双向关联 无法保存

  • hibernate

大侠们,小弟今天在项目中遇到一个问题。

就是双向关联保存的时候老报这个版本号不对错误:

2012-10-18 17:15:20,234 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - Could not synchronize database state with session
 org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.jinying.entity.user.Seller#4c816ca7cdaf4ab386de38b706cde64e]
 

关联关系:

       <one-to-one name="seller" class="com.jinying.entity.user.Seller" cascade="all" property-ref="user" ></one-to-one> 
    <many-to-one name="user" update="false" insert="false" cascade="save-update" class="com.jinying.entity.user.User" column="usid" unique="true" /> 
 
   user.setSeller(seller);
           seller.setUser(user);
           UserService userService = (UserService) ctx.getBean("userService");
           userService.save(user);

但是执行的时候并不是2个insert而是先insert user再update更新seller,如下:

Hibernate: insert into b_users (VerID, IsDel, username, password, CreateUserID, CreateUser, CreateDate, iD) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update b_sellers set VerID=?, IsDel=?, username=?, realname=?, sex=?, email=?, age=?, addr=?, postcode=?, phone=?, mobile=?, comp=?, ismain=?, idcard=?, idcardurl=?, ischeck=?, CheckUserID=?, CheckUser=?, CheckDate=?, CreateUserID=?, CreateUser=?, CreateDate=?, ModifyUserID=?, ModifyUser=?, ModifyDate=?, PermitUserID=?, PermitUser=?, PermitDate=?, wangwang=?, companyid=?, submitApply=? where iD=? and VerID=?

不知道这到底是为什么,请大侠们帮忙下,谢谢。

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答