Hibernate SQL Optimization Tips use dynamic-insert= "true" dynamic-update= "true"
Recently, I was reading Hibernate's father's masterpiece "Java Persistence with Hibernate", quite a harvest. In our familiar hibernate mapping file is also a great deal, a lot of places worth my attention.
The class tag of the hibernate mapping file uses Dynamic-insert,dynamic-update to optimize the generated Hibernate SQL statements, improve hibernate 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; Omit getter and Setter methods}
The Hibernate mapping file (User.hbm.xml, omitted file header declaration) is defined as:
<className= "model. User "table=" Users " > <idName= "id" column= "id" > <generatorclass= "native" /> </id> <propertyName= "Age" /> <propertyName= "FirstName" /> <propertyName= "LastName" /> <setName= "emailaddresses" table= "Person_email_addr" > <keyColumn= "person_id" /> <elementType= "string" column= "Email_addr" /> </set> </class> We write a test class to test Usertest.
public class usertest extends testcase { public usertest (string testname) { super (testname); } private session session; private SessionFactory 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 () { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("================testsaveuser======= =========="); User user = new user (); user.setage (29); session.save (user); &NBSP;&NBSP;&NBsp; assertnotnull ("id is assigned !", User.getId ()); &NBSP;&NBSP;&NBSP;&NBSP} public void testupdateuser () { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;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, the complete hibernate SQL statement is generated (note that the Hibernate property Show_sql is set to true).
================testsaveuser================= Hibernate:insert into the Users (age, FirstName, LastName) VALUES (?,?,?) ================testupdateuser================= Hibernate:insert into the Users (age, FirstName, LastName) VALUES (?,?,? ) hibernate:update Users set age=?, Firstname=?, Lastname=? where id=? If we add dynamic-insert= "true" dynamic-update= "true" to the <class ...>, it becomes the following.
<className= "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 hibernate SQL contain only the table field that corresponds to the modified property in the user class.
================testsaveuser================= Hibernate:insert into Users (?) ================testupdateuser================= Hibernate:insert into Users (?) Hibernate:update Users set firstname=? where id=? If a table has a complex structure and a lot of fields, using Dynamic-insert,dynamic-update can improve performance a little.
Lead http://developer.51cto.com/art/200906/130104.htm
Including Hibernate class Introduction