本文主要是介绍Hibernate中save()和persist()区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原文地址:https://developer.jboss.org/wiki/HibernateFAQ-CommonProblems
I don't know if I should use save() or persist().
Both methods make a transient instance persistent. However, the persist() method doesn't guarantee(保证) that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time.
The persist() method also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.
The save() method does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is not good in a long-running conversation with an extended Session/persistence context.
这篇关于Hibernate中save()和persist()区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!