The use of Hibernate Dynamic-insert and Dynamic-update

Source: Internet
Author: User

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

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.