<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.
- 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, omitting the file header declaration) is defined as:
- <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>
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 () {
- System.out.println ("================testsaveuser=================");
- User user = new user ();
- User.setage ();
- Session.save (user);
- Assertnotnull ("ID is assigned!") , User.getid ());
- }
- public void testupdateuser () {
- System.out.println ("================testupdateuser=================");
- User user = new user ();
- User.setage ();
- 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 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" in <class ...>, it becomes as follows.
- <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.
- ================testsaveuser=================
- Hibernate:insert into Users values (?)
- ================testupdateuser=================
- Hibernate:insert into Users values (?)
- 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/
Related:
Hibernate Update updates only modified fields
Hibernate SQL Optimization Tips dynamic-insert= "true" dynamic-update= "true"