Differences among save, update, saveOrUpdate, merge methods in Session?

后端 未结 7 2406
粉色の甜心
粉色の甜心 2020-12-04 08:50

I am new to Hibernate and went through the Hibernate tutorial last week. I have a few doubts about methods save, update, saveOrUpdate and merge in the Session class. These a

相关标签:
7条回答
  • 2020-12-04 09:25

    You are exactly right in all of your assessments. You get it.

    For your first question, if i recall correctly, save specifically does an insert. So calling save again will result in another row in the db.

    For your second question, update updates an object in the session. So if the object is in the session it will update. If the object is not in the session, you should call merge. I believe calling update for a detached instance will result in an exception.

    0 讨论(0)
  • 2020-12-04 09:32

    update() is for the detached objects , and for transient objects . if invoke on persistent objects it will throw a NonUniqueObjectException, and this will be addressed by merge().

    0 讨论(0)
  • 2020-12-04 09:33

    @Naliba has given excellent answer over Update() method.

    Hibernate life cycle in the following image helps get an idea above the methods.

    Example: Let us see merge() method situation.

    SessionFactory factory = cfg.buildSessionFactory();
    Session session1 = factory.openSession();
    
    Student student1 = null;
    Object object1 = session1.get(Student.class, new Integer(101));
    student1 = (Student)object1;
    session1.close();
    
    student1.setMarks(97);// -->object will be in some RAM location, not in the session1 cache
    
    Session session2 = factory.openSession();
    Student student2 = null;
    Object object2 = session2.get(Student.class, new Integer(101));
    student2 = (Student)object2;
    Transaction tx=session2.beginTransaction();
    
    session2.merge(student1);
    

    Above student1 is in detached state, modified that detached object student1, now if we call update() method then hibernate will throws an error.

    In this session2, we called session2.merge(s1); now into student2 object student1 changes will be merged and saved into the database.

    0 讨论(0)
  • 2020-12-04 09:34

    You should apply the differnce between save() and saveOrUpdate method in your code to get the best performance:

    The save() method returns the identifier generated by the database. On the other hand, saveOrUpdate() can do INSERT or UPDATE depending upon whether object exists in database or not. And saveOrUpdate does a select first to determine if it needs to do an insert or an update. So you should use saveOrUpdate in case update query.

    Another key difference between save() and saveOrUpdate() method is that save() method is used to make a transient object to persistent state but saveOurUpdate() can make both transient (new object) and detached (existing object) object into persistent state. So that saveOrUpdate() is often used to re-attach a detached object into Session.

    From the post Difference between save and saveOrUpdate in Hibernate

    0 讨论(0)
  • 2020-12-04 09:39

    You have most things right, but update works a little differently from how you were describing it. If an object is in the session (i.e. persistant), update is completely unnecessary. Hibernate handles persisting any changes to objects in the session when the session is flushed. Calling update on them will do nothing (and may incur a performance penalty; I'm not sure).

    Update is designed to be called on detached objects, i.e. those that are now outside the session they were loaded in. @hvgotcodes explanation seems to be incorrect, because update should only be called if the object is not in the session. update can fail if an instance of the object is already in the session. Merge should be used in that case. It merges the changes of the detached object with an object in the session, if it exists. If there's no object in the session, it will create a new one.

    So often you can avoid calling update/merge at all, but if you end up having to call one, merge handles a broader range of situations. My understanding is the only reason to use update is for better performance, assuming you know it won't error.

    This thread has a pretty good summary of some other hibernate methods, as well.

    Edit: I just thought I should say there are more differences between merge and update than I originally said. Update modifies the given entity to be persistent, whereas merge returns a new persistent entity. For merge, you're supposed to throw away the old object. Another difference is merge does a dirty check by selecting from the DB before deciding whether to write its data, whereas update always persists its data to the DB whether it's dirty or not.

    There are probably other small differences. It's always good to test Hibernate behavior out by logging the generated SQL, because the behavior doesn't always match the docs, at least in my experience.

    0 讨论(0)
  • 2020-12-04 09:43

    Merge Does Following

    Merge has intelligence. It has lot of pre-checks before it go actual merge(if required)

    1. if Object is transient, It simply fires INSERT query makes object persistent(attached to session)
    2. if Object is detached, fires select query to check whether data modified or not if modified, fires UPDATE query otherwise just ignore merge task.

    where as session.update

    1. throws exception if object is transient.
    2. if Object is detached, it simply fires UPDATE query irrespective of data changes to object.

    session.merge is expensive than update

    0 讨论(0)
提交回复
热议问题