Hibernate at initialization time, by default the SQL statement configured as a table pre-defined insert,delete,update,select (by ID) is placed in the session, where Insert,update, The select operation is all field operations on the table. If there are many fields in a table, when you do the first inser, there are more fields that are empty, or if you update a few fields frequently, you should <class> in the configuration file. The Dynamic-insert and dynamic-update elements are set to true. Their default value is False.
If you have such a table:
int, val1 varchar2 (+), Val2 varchar2 (100));
1, when the Dynamic-insert is not set
<class name= "Com.test.hb.dynamicupdate.Hbtest" table= "Hbtest" > new Hbtest (); Tbo.setid (new Integer (2)); Tbo.setval1 ("Val1"); Sessionfactory.getcurrentsession (). Save (TBO);
Some fields are empty do Insert,hibernate will use the full field Insert SQL statement, as follows:
Insert into Hbtest (VAL1, Val2,id) VALUES (?,?,?)
2, set Dynamic-insert to True, the same save, hibernate will dynamically generate SQL statements, fields without values will not appear in the INSERT statement.
<class name= "Com.test.hb.dynamicupdate.Hbtest" dynamic-insert= "true" table= "Hbtest" >INSERT INTO Hbtest (VAL1, ID) VALUES (?,?)
3, when the dynamic-update is not set
Hbtest TBO = (hbtest) sessionfactory.getcurrentsession (). Load (hbtest. Class,new Integer (1)); Tbo.setval1 ("VALXX"); Tx.commit ();
Only part of the field is updated, and hibernate still updates all the fields:
Update hbtest set val1=?,val2=? where id=?
4, the same operation if set to TRUE, the SQL statement contains only the updated fields:
Update hbtest set val1=? where id=?<class name= "Com.test.hb.dynamicupdate.Hbtest" dynamic-insert= "true" dynamic-update= "true" table= "Hbtest" >
5,hibernate This dynamic sqlupdate feature is the use of saving a snapshot when the object is loaded from the database to the hibernate session, compared to the snapshot when updating, updating only the changed values.
So the following situation is updated with all fields: The field with no value is updated to null.
New hbtest (); Tbo.setid (new Integer (1)); Tbo.setval1 ("val1zzz"); Sessionfactory.getcurrentsession (). Update (TBO);
This scenario should take advantage of the SQL support provided by hibernate and update with SQL.
The use of Hibernate Dynamic-insert and Dynamic-update