简体   繁体   English

不调用更新方法的事务性保存

[英]Transactional saves without calling update method

I have a method annotated with @Transactional.我有一个用@Transactional 注释的方法。 I retrieve an object from my Oracle DB, change a field, and then return from the method.我从我的 Oracle DB 中检索一个对象,更改一个字段,然后从该方法返回。 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?我的问题是为什么 MyObject 会被持久化到数据库?

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.此行为记录在 hibernate 参考手册的第 11 章中。 The relevant part reads:相关部分内容如下:

Hibernate defines and supports the following object states: Hibernate 定义并支持以下对象状态:

  • Transient - an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. Transient - 如果一个对象刚刚使用 new 运算符实例化,则它是瞬态的,并且它不与 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).使用 Hibernate Session 使对象持久化(并让 Hibernate 处理需要为此转换执行的 SQL 语句)。

  • 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.它可能刚刚被保存或加载,但是,根据定义,它在 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. Hibernate 将检测对处于持久状态的对象所做的任何更改,并在工作单元完成时将状态与数据库同步。 Developers do not execute manual UPDATE statements, or DELETE statements when an object should be made transient.开发人员不执行手动 UPDATE 语句,或当对象应变为瞬态时 DELETE 语句。

  • Detached - a detached instance is an object that has been persistent, but its Session has been closed. Detached - 分离的实例是一个已经持久化的对象,但它的 Session 已经关闭。 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.一个分离的实例可以在稍后的时间点重新附加到一个新的 Session,使其(以及所有修改)再次持久化。 This feature enables a programming model for long running units of work that require user think-time.此功能为需要用户思考时间的长时间运行的工作单元启用编程模型。 We call them application transactions, ie, a unit of work from the point of view of the user.我们称它们为应用事务,即从用户的角度来看的一个工作单元。

If you're using a JPA, than the specification says that if your Entity is in managed state (and this is what you do by fetching the data from the DAO within an active transaction), all changed made to it will be reflected in the database during the transaction commit .如果您使用的是 JPA,那么规范说明如果您的实体处于托管状态(这就是您通过在活动事务中从 DAO 获取数据来执行的操作),对它所做的所有更改都将反映在事务提交期间的数据库。

So, in other words - it doesn't really matter if you invoke update operation or not because the transaction commit will flush the changes to the database.所以,换句话说-这并不重要,如果你调用更新操作或不是因为事务提交将刷新对数据库的更改。

I found preventing automatic updates to the database is a two step process.我发现阻止自动更新数据库是一个两步过程。

Step I: :步骤一::

getSession().setFlushMode(FlushMode.MANUAL) // [FlushMode.NEVER is depracated in 4.x]

Step II :步骤二:

getSession().clear(); //This will actually discard all changes

我用@Transactional(readOnly = true)来解决它

For JPA, call entityManager.detach(entity) to avoid auto flush.对于 JPA,调用entityManager.detach(entity)以避免自动刷新。 But you need to be awared that the detached entity will lost ORM magic after, eg lazy fetching, cascade update.但是你需要注意分离的实体会在延迟获取、级联更新等之后失去 ORM 的魔力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @Transactional 方法调用另一个没有@Transactional 注释的方法? - @Transactional method calling another method without @Transactional anotation? 弹簧靴。 @Transactional 方法调用 @Transactional(readonly=true) 方法 - Spring Boot. @Transactional method calling a @Transactional(readonly=true) method 更新实体交易方法列表 - Update list of entities transactional method 在 Spring Boot 应用程序的 @Transactional 方法中调用 flush() - Calling flush() in @Transactional method in Spring Boot application 测试@Transactional 服务方法 JUnit - 无数据库 - Testing @Transactional service method JUnit - without database Java:从私有方法调用公共事务方法 - Java: calling public transactional method from a private method 在另一个实例中从事务方法调用 spring 非事务方法时,事务是否得到传播? - When calling a spring Non-Transactional method from a Transactional Method in another instance, does the transaction get propagated? 事务回滚后调用非事务方法 - Calling non-transactional method after transaction is rolled back @Transactional方法调用-我们应该将对象作为参数传递吗? - @Transactional Method Calling - should we pass objects as parameters? 如何在相同和不同服务中调用@Transactional和non-Transactional方法时回滚事务? - How to rollback transaction on calling @Transactional and non-Transactional method in same and different Service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM