java 使用休眠时数据库中的 save() 和 commit() 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34311069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between save() and commit() in a database while using hibernate
提问by Rameshwar Bhaskaran
I came across this example from a book while learning about the Hibernate framework.
我在学习 Hibernate 框架时从一本书中看到了这个例子。
public class BasicMovieManager()
{
private void persistMovie(Movie movie)
{
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(movie);
session.getTransaction().commit();
}
}
I can understand that the Movie object has to be mapped and written to the database. I also understand that the commit step will write to the database. But what is the purpose of save()
here? A few sources I referred say that save()
persiststhe data. Doesn't persist mean writing to a permanent storage? If not,what exactly does it mean?
我可以理解必须将 Movie 对象映射并写入数据库。我也明白提交步骤将写入数据库。但save()
这里的目的是什么?我提到的一些来源说可以save()
保留数据。不持久意味着写入永久存储吗?如果不是,那究竟是什么意思?
回答by shaydel
I Believe the comparison is misplaced,you should compare
我相信比较是错位的,你应该比较
Commit
vs Flush
Commit
对比 Flush
and
和
Save
vs Persist
Save
对比 Persist
Edited:
编辑:
You should know this:
你应该知道这一点:
transient: never persistent, not associated with any Session.
persistent: associated with a unique Session.
detached: previously persistent, not associated with any Session.
Commit
will save the data to DB, so you cannot rollback anymore, in opposed toFlush
.Save
will generate and return identifier prior to writing the object, later uponFlush
orCommit
it writes the data to the database.Where
Persist
will not return a value,as you only mark the object as dirty in the cache, so upon flush or commit it will be saved, this is useful when persisting multiple objects in a transaction.
Commit
将数据保存到数据库,所以你不能再回滚,而不是Flush
.Save
将生成并在标识符写入对象之前返回,之后Flush
或Commit
它的数据写入到数据库。where
Persist
不会返回值,因为您只在缓存中将对象标记为脏对象,因此在刷新或提交时将保存它,这在事务中持久化多个对象时很有用。
回答by Anders R. Bystrup
Quick answer: save()
stores the data in the database. commit()
makes it visible to others (cf. isolation levels).
快速回答:save()
将数据存储在数据库中。commit()
使其对其他人可见(参见隔离级别)。
Slightly longer answer: Database operations should obey the ACID principle, (A)tomicity being the operative element here. If you are doing more than one change/insert, you can wrap it in a transaction and commit/reject the entire set of operations as a whole.
稍微长一点的答案:数据库操作应该遵循ACID 原则,(A)tomicity 是这里的操作元素。如果您正在进行多个更改/插入,您可以将其包装在一个事务中,并将整个操作集作为一个整体提交/拒绝。
In your example it doesn't make much sense to start a transaction, but in real-life situations it very much makes sense.
在您的示例中,开始交易没有多大意义,但在现实生活中,这非常有意义。
Cheers,
干杯,
回答by Zia
save method,Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.) This operation cascades to associated instances if the association is mapped with cascade="save-update".its associated with session
save 方法,持久化给定的瞬态实例,首先分配一个生成的标识符。(或者,如果使用分配的生成器,则使用标识符属性的当前值。)如果关联被映射到关联实例,则该操作级联到关联实例。
commit method,Flush the associated Session and end the unit of work (unless we are in FlushMode.NEVER. This method will commit the underlying transaction if and only if the underlying transaction was initiated by this object.Commit will make the database commit. The changes to persistent object will be written to database.transaction.commit() does flush the session, but it also ends the unit of work.its associated with transaction
commit 方法,刷新关联的 Session 并结束工作单元(除非我们处于 FlushMode.NEVER。当且仅当底层事务由该对象发起时,此方法才会提交底层事务。Commit 将使数据库提交。对持久对象的更改将写入 database.transaction.commit() 确实刷新会话,但它也会结束与事务关联的工作单元
for more details on these method please below link,
有关这些方法的更多详细信息,请点击以下链接,
回答by jcool
Basically transactions are used when you are trying to persist related set of objects. If you are trying to insert only one object then transaction is not necessary. But when you are trying to persist a set of related objects which are dependent on each other then you should go for transaction where it comes handy
当您尝试持久化相关对象集时,基本上会使用事务。如果您尝试仅插入一个对象,则不需要事务。但是当你试图保留一组相互依赖的相关对象时,你应该去方便的事务
As for example:
例如:
//1.Load session
//2. persist an object
In above scenario nothing will happen if your persisting of a single object fails or success but when you will do like this:
在上述情况下,如果您对单个对象的持久化失败或成功,则不会发生任何事情,但是当您这样做时:
//1. Load session
//2. Persist one object
//3. Persist other object whose data affects previous
In above scenarion suppose second was performed successfully but third failed that can adversely affect your data or business. This can be resolved as:
在上述场景中,假设第二个成功执行但第三个失败,这可能会对您的数据或业务产生不利影响。这可以解决为:
//1. Load session
//2. Begin transaction
//3. perform set of related operation
//4. commit
If any thing will go wrong in above scenario the whole transaction will be rollbacked nothing will be persisted. And if you want to do something after your transaction fails you can handle it by using try catch.
如果在上述场景中出现任何问题,整个事务将被回滚,任何事情都不会被持久化。如果您想在交易失败后做某事,您可以使用 try catch 来处理它。
So, basically save()
is used to save data in tables but commit()
is used in transaction management
所以,基本上save()
用于将数据保存在表中但commit()
用于事务管理