简体   繁体   English

如何使用Spring HibernateOperations对隐式事务进行简单操作?

[英]How can I use Spring HibernateOperations for simple operations with a implicit transaction?

I currently make heavy use of HibernateOperations in Spring for working with Hibernate. 我目前在Spring中大量使用HibernateOperations与Hibernate一起工作。 Some of the time I use a TransactionTemplate to demark a transaction explicitly. 有时,我使用TransactionTemplate显式标记事务。 However sometimes I just want to save/update a single object. 但是有时候我只想保存/更新一个对象。 If I just call hibernateOperations.save() with no explicit transaction active then it sort of works (I think auto-commit is coming in to play here) but it means certain events don't fire, like I have a hibernate event listener hooked into the commit transaction which I am missing in this circumstance. 如果我只是在没有显式事务激活的情况下调用hibernateOperations.save(),那么它可以正常工作(我认为自动提交即将在此处播放),但是这意味着某些事件不会触发,例如我挂了一个休眠事件监听器进入这种情况下我缺少的提交事务。

What I am really looking for is a way to easily do something hibernateOperations.saveAndCommit() as I don't really want to have the whole TransactionTemplate + callback noise for simple things like a single save. 我真正想要的是一种轻松地执行hibernateOperations.saveAndCommit()的方法,因为我真的不希望为单个保存之类的简单事情拥有整个TransactionTemplate +回调噪声。

I could probably roll my own, just wondering if there's an existing solution/best practice for this. 我可能会提出自己的建议,只是想知道是否存在现有的解决方案/最佳实践。

I am not interested in driving my transactions via annotations at this point, I prefer the explicitness of the code. 我现在对通过注释推动交易不感兴趣,我更喜欢代码的明确性。

Any suggestions welcome. 任何建议欢迎。

Cheers. 干杯。

尝试使用声明式事务处理方法 ,一次设置有点麻烦,但是之后您不必再担心事务处理了……

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM