) Dynamic insert and dynamic update in hibernate

Source: Internet
Author: User

From: http://www.blogjava.net/ptah/archive/2010/01/02/308031.html

Using Dynamic-insert and dynamic-update in the class tag of the hibernate ing file can optimize the generated SQL statements, improve SQL Execution efficiency, and ultimately improve system performance.
For example, there is a user class.

Public class user {

/** Creates a new instance of user */
Public user (){
}
Private long ID;
Private int age;

Private string firstname;
Private string lastname;
Private set emailaddresses;
// The getter and setter methods are omitted.
}

The Hibernate ing file (user. HBM. XML, omitting the file header Declaration) is defined:
<Hibernate-mapping>
<Class name = "model. User" table = "users">
<ID name = "ID" column = "ID">
<Generator class = "native"/>
</ID>
<Property name = "Age"/>
<Property name = "firstname"/>
<Property name = "lastname"/>

<Set name = "emailaddresses" table = "person_email_addr">
<Key column = "person_id"/>
<Element type = "string" column = "email_addr"/>
</Set>
</Class>
</Hibernate-mapping>
Let's write a test class to test usertest.

Public class usertest extends testcase {

Public usertest (string testname ){
Super (testname );
}

Private session;
Private sessionfactory;

Protected void setup () throws exception {
Sessionfactory = hibernateutil. getsessionfactory ();
Session = sessionfactory. opensession ();
Session. gettransaction (). Begin ();
}

Protected void teardown () throws exception {
Session. gettransaction (). Commit ();
Session. Close ();

}

/**
* Test of getage method, of Class model. User.
*/
Public void testsaveuser (){
System. out. println ("====================== testsaveuser ========================= ");

User user = new user ();
User. setage (29 );
Session. Save (User );
Assertnotnull ("ID is assigned! ", User. GETID ());
}
Public void testupdateuser (){
System. out. println ("======================= testupdateuser ======================= ");

User user = new user ();
User. setage (29 );
Session. Save (User );
Assertnotnull ("ID is assigned! ", User. GETID ());

User _ User = (User) Session. Get (user. Class, user. GETID ());
_ User. setfirstname ("array ");
Session. Update (_ User );

}


}
After the test is run, a complete SQL statement is generated (note that the show_ SQL attribute of Hibernate is set to true ).

==================== Testsaveuser ================================
Hibernate: insert into users (age, firstname, lastname) values (?, ?, ?)
==================== Testupdateuser ================================
Hibernate: insert into users (age, firstname, lastname) values (?, ?, ?)
Hibernate: Update users set age = ?, Firstname = ?, Lastname =? Where id =?

If we add dynamic-insert = "true" dynamic-update = "true" in <class...>, it is changed to the following.

<Class name = "model. User" table = "users" dynamic-insert = "true" dynamic-update = "true">

Run the test class again and you will find that the fields involved in the generated SQL only contain the table fields corresponding to the attributes modified in the user class.

==================== Testsaveuser ================================
Hibernate: insert into users (AGE) values (?)
==================== Testupdateuser ================================
Hibernate: insert into users (AGE) values (?)
Hibernate: Update users set firstname =? Where id =?

If the structure of a table is complex and there are many fields, using dynamic-insert and dynamic-update can slightly improve the performance.

If you load a po in session1 and update it in session2, I'm sorry.
So can I use cross-sessionDynamic-UpdateWhat about it? Yes
Use Merge ()

 

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.