事务保存不调用更新方法 [英] Transactional saves without calling update method

查看:87
本文介绍了事务保存不调用更新方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用@Transactional注解的方法。我从Oracle DB中检索一个对象,更改一个字段,然后从该方法返回。

applicationContext

我忘了保存对象,但发现数据库会被更新。 < tx:annotation-driven /> 
< bean id =transactionManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>

我的方法

  @Transactional 
public void myMethod(long id){
MyObject myObj = dao.getMstAttributeById(id);
myObj.setName(new name);
//dao.update(myObj);
}

我的问题是为什么MyObject会持久保存到数据库?

解决方案

因为hibernate会自动检测对 persistent 实体所做的更改并相应地更新数据库。此行为记录在hibernate参考手册的第11章中。相关部分的内容如下:

lockquote
Hibernate定义并支持以下对象状态:

    >
  • Transient - 如果一个对象刚刚使用new运算符进行实例化,并且与Hibernate Session没有关联,则该对象是暂时的。它在数据库中没有持久表示并且没有分配标识符值。如果应用程序不再拥有引用,则垃圾收集器将销毁瞬态实例。使用Hibernate Session使对象持久化(并让Hibernate处理需要为此转换执行的SQL语句)。

  • 持久性 - 持久性实例在数据库中具有表示形式和标识符值。它可能只是被保存或加载,但是,它被定义在Session的范围内。 Hibernate将检测对处于持久状态的对象所做的任何更改,并在工作单元完成时将状态与数据库同步。开发人员不会执行手动UPDATE语句或DELETE语句。
  • - 分离的实例是一直持续的对象,但其Session已关闭。当然,该对象的引用仍然有效,并且分离的实例甚至可能会在此状态下进行修改。分离的实例可以在稍后的时间点重新附加到新的会话中,使得它(和所有的修改)再次持久。此功能为需要用户思考时间的长时间运行的工作单元启用编程模型。我们称之为应用程序事务,即从用户的角度来看是一个工作单元。

  • I have a method annotated with @Transactional. I retrieve an object from my Oracle DB, change a field, and then return from the method. I forgot to save the object, but discovered that the database gets updated anyway.

    applicationContext

    <tx:annotation-driven />
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    

    my method

    @Transactional
    public void myMethod(long id) {
        MyObject myObj = dao.getMstAttributeById(id);
        myObj.setName("new name");
        //dao.update(myObj);
    }
    

    my question is why does MyObject get persisted to the database?

    解决方案

    Because hibernate will automatically detect changes made to persistent entities and update the database accordingly. This behaviour is documented in chapter 11 of the hibernate reference manual. The relevant part reads:

    Hibernate defines and supports the following object states:

    • Transient - an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore. Use the Hibernate Session to make an object persistent (and let Hibernate take care of the SQL statements that need to be executed for this transition).

    • Persistent - a persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded, however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. Developers do not execute manual UPDATE statements, or DELETE statements when an object should be made transient.

    • Detached - a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.

    这篇关于事务保存不调用更新方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆