我们项目用的spring2+hibernate3
然后在代码里我用getHibernateTemplate().save(object)方法保存一个对象,接着再改变这个object里的值再getHibernateTemplate().update的时候 报错了,是这个object不再在session里了。事物是spring管理的。
请问下怎么能让这个object继续保存在session里呢?

解决方案 »

  1.   

    报什么错误?
    hibernate的Object一直保存着的,除非你session.clear()获得evit(object);
      

  2.   

    报错信息:
    org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.ccit.csw.t2.model.NcsRealprocT2cwopenRecorde] with identifier [100000091]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.ccit.csw.t2.model.NcsRealprocT2cwopenRecorde#100000091]
    Caused by: 
    org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.ccit.csw.t2.model.NcsRealprocT2cwopenRecorde#100000091]
      

  3.   

    我没有显示的调用  session.clear(),evit(object);
      

  4.   

     Row was updated or deleted by another transaction你的第一个事物save还有提交,不能update
      

  5.   

    这是DAO层实现类里的两个方法 public void save(NcsRealprocT2cwopenRecorde transientInstance) throws BaseException {
    getHibernateTemplate().saveOrUpdate(transientInstance);
    }
    public void update(NcsRealprocT2cwopenRecorde transientInstance) throws BaseException  {
    //getHibernateTemplate().load(transientInstance,transientInstance.getT2cwRecordeId());
    //updateEntity(transientInstance);
    getHibernateTemplate().saveOrUpdate(transientInstance);
    }=============================================
    action里的代码:
    comboT2Service.insertRecord(recordVo);
    //中间有些改变 recordVo 的值的代码
    //更新数据
    comboT2Service.updateRecord_NumHandle(recordVo, baseVo);
    ==============================================
    上面两个方法都是在服务层里定义的,实现就是直接调上面DAO里的方法
      

  6.   

    你在spring里面配置声明式事物管理没有?
      

  7.   

    <!-- 配置事务拦截器 -->
    <bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <!-- 事务拦截器bean需要依赖注入一个事务管理器  -->
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
    <!-- 下面定义事务传播属性 -->
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>
    就这么一段,我不懂,是别人配的
      

  8.   

    还有这一段:
    <!-- 配置事务管理器,使用Hibernate局部事务管理器策略 -->
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    类配置进去?什么意思?现在就是数据保存进去了,如果在update方法前加一句load的时候就可以更新到数据了。所以我怀疑是save后对象编程临时的了,你说的svae没有提交也有可能。不过没提交为什么数据库里有数据?