Reputation: 1669
From the link below:
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), this INSERT happens immediately, no matter if you are inside or outside of a transaction.
http://javarevisited.blogspot.com/2012/09/difference-hibernate-save-vs-persist-and-saveOrUpdate.html
I have this doubt: how can save method insert immediately even if it is outside of transaction? Because if insert happens that means it is hitting the database and how can be a database operation be without any transaction? AFAIK the smallest granularity of a transaction is a single insert/update/select/delete statement. So I need clarification.
Upvotes: 3
Views: 3727
Reputation: 6265
When an entity is retrieved from the database, it is attached to the Session and remains part of the Session until the Session (and its transaction) ends. Likewise, when an entity is added or updated, those changes are also attached to the Session, and when an entity is deleted from the database, it is removed from the Session.
However, that statement of yours (or whatever that link you provided) is true.
Hibernate save()
can be used to save entity to database. We can invoke this method outside a transaction. If we use this without transaction and we have cascading between entities, then only the primary entity gets saved unless we flush the session.
You can find a working example here:
Upvotes: 2