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

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

问题描述

我有一个用@Transactional 注释的方法.我从我的 Oracle DB 检索一个对象,更改一个字段,然后从该方法返回.我忘了保存对象,但发现数据库无论如何都会更新.

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.

应用上下文

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

我的方法

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

我的问题是为什么 MyObject 会被持久化到数据库中?

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

推荐答案

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

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 定义并支持以下对象状态:

Hibernate defines and supports the following object states:

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

  • 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 - 一个持久化实例在数据库中有一个表示和一个标识符值.它可能刚刚被保存或加载,但是,根据定义,它在 Session 的范围内.Hibernate 将检测对处于持久状态的对象所做的任何更改,并在工作单元完成时将状态与数据库同步.开发人员不执行手动 UPDATE 语句,或在对象应变为瞬态时执行 DELETE 语句.

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 - 分离的实例是一个持久化的对象,但它的 Session 已经关闭.当然,对对象的引用仍然有效,并且在这种状态下甚至可能会修改分离的实例.分离的实例可以在稍后的时间点重新附加到新的 Session,使其(以及所有修改)再次持久化.此功能为需要用户思考时间的长时间运行的工作单元启用编程模型.我们称它们为应用事务,即从用户的角度来看的一个工作单元.

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天全站免登陆