Hibernate's Merge () method

Source: Internet
Author: User
or a long time ago contacted Hibernate, the company's project has been used Ibatis, yesterday to do outside the project encountered a problem:

   Laborperson.setid (Laborpersonid);
   ...
   Laborperson Oldlaborperson = (Laborperson) Getdao (). Getobjbyid (Laborperson.class, Laborpersonid);
   Laborperson.setcreatetime (Oldlaborperson.getcreatetime ());
   Laborperson.setcreateuser (Oldlaborperson.getcreateuser ());
   Laborperson.setunit (Oldlaborperson.getunit ());
   Oldlaborperson = null;


Saveorupdate times wrong a different object with the same identifier value is already associated with the session

In fact, just want to change the original old data to save the creation time and other fields, friends told me to take out the old data after a set go in, dozens of fields Ah, think before there seems to be a way to merge, find an article to try, can be normal updated (no saveorupdate, Directly according to Laborpersonid to determine whether to insert or update, update with the merge, hibernate continue to grope ...

  if (Stringutil.isnotnullorblank (Laborpersonid))
{  Laborperson Oldlaborperson = (Laborperson) GetDao ().    Getobjbyid (Laborperson.class, Laborpersonid);  Laborperson.setcreatetime (Oldlaborperson.getcreatetime ());  Laborperson.setcreateuser (Oldlaborperson.getcreateuser ());  ...      hibernateutil.getsession (). Merge (Laborperson);
}
{
laborperson.setcreatetime (new Date ());
Laborperson.setcreateuser (curuser);
...       
Hibernateutil.getsession (). Save (Laborperson);
}

Original: http://littie1987.iteye.com/blog/1039082 Hibernate's Merge () method Hibernate SQL

Here's how to hibernate the merge method. I'm going to follow the three states of the Hibernate object lifecycle.

1: If the Pojo object is in a free State, I mean the Free State is that the object's ID value is null. Hibernate determines whether an object exists in the database, not other information about the object, but rather determines if the ID exists in the database. If the ID is empty, it naturally does not exist, so when we call the merge method, the insert operation is performed directly. This is a bit like the Saveorupdate () method.    Read a piece of code: Java Code User user = new user ();    User.setid (4);    User.setusername ("Heyuanling2");    User.setage (23);    User.setsex ("w");    User.setpassword ("heyuanling");    Session session = This.getsession ();    Transaction tr = session.begintransaction ();    User Exituser = (user) Session.get (user.class, New Integer (1));    Session.merge (user); Tr.commit ();

		User user = new user ();
		User.setid (4);
		User.setusername ("Heyuanling2");
		User.setage (a);
		User.setsex ("w");
		User.setpassword ("heyuanling");
		Session session = This.getsession ();
		Transaction tr = session.begintransaction ();
		User Exituser = (user) Session.get (user.class, New Integer (1));
		Session.merge (user);
		Tr.commit ();

See Hibernate SQL statements again: Java code hibernate:         select             max (ID)          from             user_    hibernate:          insert         into             user_             (username, password,  Sex, age, birthday, other, id)          values             (?,  ?, ?, ?, ?, ?, ?)   

Hibernate: 
    Select
        max (ID) 
    from
        user_
Hibernate: 
    insert 
    into
        user_
        ( Username, password, sex, age, birthday, and other, id 
    values
        (?,?,?,?,?,?,?)

Two: The//user.setid: If we take out the comments in the above code (4), then it becomes the object of the removal of the pipe (in fact, from the free to remove the tube is so simple, no official said so knew ...). This is what we're going to see. SQL printing of the console: Java code hibernate:         select             user0_.id as id4_0_,             user0_.username as username4_0_,             user0_.password as password4_0_,             user0_.sex as sex4_0_,            user0_.age  as age4_0_,            user0_.birthday as  birthday4_0_,            user0_.other as  other4_0_         from             user_ user0_         where            user0_.id=?   

Hibernate: 
    Select
        user0_.id as id4_0_,
        user0_.username as username4_0_,
        User0_.password as Password4 _0_,
        user0_.sex as sex4_0_,
        user0_.age as age4_0_,
        User0_.birthday as birthday4_0_,
        User0_.other as other4_0_ 
    from
        user_ user0_ 
    where
        user0_.id=?

See no, because ID is not empty, so hibernate will not insert again. Since the object's information is exactly the same as that in the database, hibernate only executes a SELECT statement without update, and if we make a slight change to the value of the field, the SQL statement that the console prints should also have an UPDATE statement. At this point, the merge is the same as the Saveorupdate () method.

Third: Persistent state: A better understanding of the persistent state. If we get a record from the database, then the record is in a persistent state, and if we call the merge again, then Hibernate will determine if the record is modified, nothing is done, and the update is updated. This is a bit like saveorupdate ().    Java code Session session = This.getsession ();    Transaction tr = session.begintransaction ();    User Exituser = (user) Session.get (user.class, New Integer (1));    Exituser.setage (11);    Session.merge (Exituser);    Tr.commit (); Session.close ();

Session session = This.getsession ();
Transaction tr = session.begintransaction ();
User Exituser = (user) Session.get (user.class, New Integer (1));
Exituser.setage (one);
Session.merge (exituser);
Tr.commit ();
Session.close ();

  See console print results: Java code hibernate:         select             user0_.id as id4_0_,             user0_.username as username4_0_,             user0_.password as password4_0_,             user0_.sex as sex4_0_,            user0_.age as  age4_0_,            user0_.birthday as  birthday4_0_,            user0_.other as other4_0_          from            user _ user0_         where          &Nbsp; user0_.id=?    hibernate:         update             user_         set             username=,            password=?            sex=             age=,            birthday=?,            other=?         where             id=?  

Hibernate: 
    Select
        user0_.id as id4_0_,
        user0_.username as username4_0_,
        User0_.password as Password4 _0_,
        user0_.sex as sex4_0_,
        user0_.age as age4_0_,
        User0_.birthday as birthday4_0_,
        User0_.other as other4_0_ 
    from
        user_ user0_ 
    where
        user0_.id=?
Hibernate: 
    update
        user_ 
    set
        username=?,
        password=?,
        sex=?,
        age=?
        birthday=,
        other=? 
    where
        id=?

If no changes are made to the record, there is no subsequent UPDATE statement.

So what's the difference between merge and Saveorupdate ()? Look at a piece of code: Java Code session session = this.getsession ();    transaction tr = session.begintransaction ();    user exituser =  (User) Session.get (User.class, new integer (1));    Tr.commit ();    Session.close ();    session = getsession ();    tr = session.begintransaction ();    user exituser2 =  (User) Session.get (User.class, new integer (1));    session.update (Exituser);    Tr.commit ();    session.close ();  

Session session = This.getsession ();
Transaction tr = session.begintransaction ();
User Exituser = (user) Session.get (user.class, New Integer (1));
Tr.commit ();
Session.close ();
Session = GetSession ();
TR = session.begintransaction ();
User Exituser2 = (user) Session.get (user.class, New Integer (1));
Session.update (exituser);
Tr.commit ();
Session.close ();

Running the code above, hibernate us an error: A different object with the same identifier value is already associated with the session. This means that it is not possible to identify the same object in the session cache with two identities. So, what happens when update is changed into a merge? After the merge, all OK, run normally. In fact, the merge merges two objects with the same identifier before performing the update, in the direction of merging to Exituser2.

Note: The merge method goes back to the cache before execution to find out if there is a corresponding record, that is, there will be a SELECT statement, the purpose of the change statement is to determine whether the object has been modified. and update doesn't care about this, just a single UPDATE statement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.