Hibernate SQL Optimization Tips dynamic-insert= "true" dynamic-update= "true"

Source: Internet
Author: User

<java persistence with Hibernate>, who was reading the father of Hibernate recently, was quite fruitful.
In our familiar hibernate mapping file There is also a lot of space, many worthy of my attention.
The class tag of the hibernate mapping file uses Dynamic-insert,dynamic-update to optimize the generated SQL statements, improve the efficiency of SQL execution, and ultimately improve system performance.
For example, there is a user class.

  1. Public class User {
  2. /** Creates a new instance of User * /
  3. public User () {
  4. }
  5. private long ID;
  6. private int age ;
  7. private String FirstName;
  8. private String LastName;
  9. private Set emailaddresses;
  10. Omit getter and Setter methods
  11. }

The Hibernate mapping file (User.hbm.xml, omitting the file header declaration) is defined as:

  1. <hibernate-mapping>
  2. <class name="model. User " table=" Users " >
  3. <ID name="id" column="id">
  4. <generator class="native"/>
  5. </ID>
  6. <property Name=' age '/>
  7. <property Name="FirstName"/>
  8. <property Name="LastName"/>
  9. <set name="emailaddresses" table="Person_email_addr">
  10. <key column="person_id"/>
  11. <element Type="string" column="email_addr"/>
  12. </Set>
  13. </class>
  14. </hibernate-mapping>

We write a test class to test Usertest.

  1. Public class usertest extends TestCase {
  2. public usertest (String testname) {
  3. Super(testname);
  4. }
  5. private Session session;
  6. private sessionfactory sessionfactory;
  7. protected void setUp () throws Exception {
  8. Sessionfactory=hibernateutil.getsessionfactory ();
  9. Session=sessionfactory.opensession ();
  10. Session.gettransaction (). Begin ();
  11. }
  12. protected void TearDown () throws Exception {
  13. Session.gettransaction (). commit ();
  14. Session.close ();
  15. }
  16. /**  
  17. * Test of Getage method, of class model. User.   
  18. */  
  19. public void testsaveuser () {
  20. System.out.println ("================testsaveuser=================");
  21. User user = new user ();
  22. User.setage ();
  23. Session.save (user);
  24. Assertnotnull ("ID is assigned!")   , User.getid ());
  25. }
  26. public void testupdateuser () {
  27. System.out.println ("================testupdateuser=================");
  28. User user = new user ();
  29. User.setage ();
  30. Session.save (user);
  31. Assertnotnull ("ID is assigned!")   , User.getid ());
  32. User _user= (user) Session.get (user).   Class, User.getid ());
  33. _user.setfirstname ("Array");
  34. Session.update (_user);
  35. }
  36. }

After the test is run, a complete SQL statement is generated (note that the Hibernate property Show_sql is set to true).

    1. ================testsaveuser=================
    2. Hibernate:insert into the Users (age, FirstName, LastName) VALUES (?,?,?)
    3. ================testupdateuser=================
    4. Hibernate:insert into the Users (age, FirstName, LastName) VALUES (?,?,?)
    5. Hibernate:update Users set age=?, Firstname=?, Lastname=? where id=?

If we add dynamic-insert= "true" dynamic-update= "true" in <class ...>, it becomes as follows.

    1. <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 contain only the table field for the modified property in the user class.

    1. ================testsaveuser=================
    2. Hibernate:insert into Users values (?)
    3. ================testupdateuser=================
    4. Hibernate:insert into Users values (?)
    5. Hibernate:update Users set firstname=? where id=?

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


Hibernate mapping file, the class element can be defined in the  
dynamic-update= "True|false" &NBSP
dynamic-insert= "True|false"  

Dynamic-update (optional, false by default): Specifies that SQL for update will be dynamically generated at run time, and update only those fields that have changed.  

Dynamic-insert (optional, false by default): Specifies that SQL for insert is dynamically generated at run time and contains only those non-null value fields.  

Note that dynamic-update and Dynamic-insert settings are not inherited to subclasses, so the <subclass> or <joined-subclass> element may need to be set again.  

Performance issue: The SQL UPDATE statement is pre-generated, and if you add dynamic, you need to scan each property's changes each time you update, and then generate an update, which has a slightly more efficient effect.  
If you don't have a special requirement, it's a good default.  

If you update multiple records at one time, hibernate will not be able to use ExecuteBatch for batch updates, which is much less efficient. Also, in this case, multiple SQL means that the database has to do multiple SQL statement compilation.  

Whether or not to use:  
Specific problems specifically analyzed, if a table field is more, and often just update a record of one or two fields, then dynamic update will be more effective. and the generated SQL statements are easy to understand.  

Transferred from: http://blog.163.com/ma_yaling/blog/static/245367201051854849268/


Hibernate Update updates only modified fields

Hibernate SQL Optimization Tips dynamic-insert= "true" dynamic-update= "true"

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.