java – 事务性保存而不调用update方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 事务性保存而不调用update方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2252字,纯文字阅读大概需要4分钟。
内容图文
![java – 事务性保存而不调用update方法](/upload/InfoBanner/zyjiaocheng/713/a27659c4ef4d44a5a3473ece04b72d11.jpg)
我有一个使用@Transactional注释的方法.我从Oracle DB中检索一个对象,更改一个字段,然后从该方法返回.我忘了保存对象,但发现无论如何都要更新数据库.
的applicationContext
<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被持久化到数据库?
解决方法:
因为hibernate会自动检测对持久性实体所做的更改并相应地更新数据库.这种行为记录在chapter 11的hibernate参考手册中.相关部分如下:
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.
内容总结
以上是互联网集市为您收集整理的java – 事务性保存而不调用update方法全部内容,希望文章能够帮你解决java – 事务性保存而不调用update方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。