Rameshwar Bhaskaran
Rameshwar Bhaskaran

Reputation: 345

Difference between save() and commit() in a database while using hibernate

I came across this example from a book while learning about the Hibernate framework.

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() persists the data. Doesn't persist mean writing to a permanent storage? If not,what exactly does it mean?

Upvotes: 3

Views: 17478

Answers (3)

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16060

Quick answer: save() stores the data in the database. commit() makes it visible to others (cf. isolation levels).

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.

In your example it doesn't make much sense to start a transaction, but in real-life situations it very much makes sense.

Cheers,

Upvotes: 2

shaydel
shaydel

Reputation: 589

I Believe the comparison is misplaced,you should compare

Commit vs Flush

and

Save vs 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.
  1. Commit will save the data to DB, so you cannot rollback anymore, in opposed to Flush.

  2. Save will generate and return identifier prior to writing the object, later upon Flush or Commit 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.

Upvotes: 3

jcool
jcool

Reputation: 324

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.

So, basically save() is used to save data in tables but commit() is used in transaction management

Upvotes: 0

Related Questions