3

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?

5
  • 1
    Save method stores an object into the database. That means it insert an entry if the identifier doesn’t exist, else it will throw error. If the primary key already present in the table, it cannot be inserted. Commit will make the database commit. The changes to persistent object will be written to database. Commented Dec 16, 2015 at 11:46
  • Suppose the save() step failed . If I try to commit then, what would happen? Shouldn't I use exception handling in the save() step? Commented Dec 16, 2015 at 11:49
  • If save() fails, then the database will not be committed.. It will throw an exception.. And yes.. you should catch the exception.. Commented Dec 16, 2015 at 11:51
  • So should I use a try-catch block around the save() step or is it taken care of? Commented Dec 16, 2015 at 11:51
  • Yes.. You should use try-catch block on the whole session. Commented Dec 16, 2015 at 11:53

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't answer my question, I am sorry. If both "save to the database" , why do we use both of them together in this example?
Commit means save the cache changes to the database, Save only save that single object(in the cache),it doesn't care about other dirty objects in the cache, and later on all dirty objects will be written to the database. if you avoid save\persist how would you save that single object to the cache\DB,and if you avoid Commit\Flush how will you be able to verify that all cache was persisted to the DB.
Example: think about typing a text document, when you type in the letter(save\persist) you only save that to an in-memory object. but when you press ctrl+s you flush\commit the data,into a file(the DB).
The last comment nailed it..Thanks! :)
2

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,

2 Comments

Did you mean commit() instead of persist() ?
Arrgh. Yes. Sorry. Thanks. Fixed.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.