Hibernate update and updates part of the field _hibernate

Source: Internet
Author: User
Updates Session.update (object)

1. To update the detached object, after the update is completed to persistent state

2. Update Transient object will complain

3. Update your own set ID of the transient object can (database has corresponding records)

4. Persistent state of the object as long as the set (such as: T.setname ... ) different fields will be updated


Hibernate if the session.update (Object o) is used directly; or session.updateorupdate (Object o); Will update all the fields in this table.

Such as:

[Java] view plaincopyprint? experclass4k e = new experclass4k ();                e.settime (time);                e.setq_num (q_num);                E.SETK (k);                if  (str ==  "Finch_fix")  {                    e.setfinch_fix_cost1 (cost1);                    e.setfinch_fix_cost2 (Cost2 );                    E.setfinch_fix_cost (cost1 + cost2);               } else if  (str ==  "my")  {                    e.setmy_cost1 (Cost1) ;                    E.setmy_cost2 (cost2);                    e.setmy_cost (cost1 + cost2);                    }                //session.save (e);                session.saveorupdate (e);   I wanted to make a different update strategy based on STR, but for the same time,hibernate SQL statement to change all fields once (without the value of set, Hibernate will be directly assigned null).

So how do we change only the fields we update?

There are four different ways:

The property label update = "false" is set in 1.XML, as follows: We set the age to not make changes in the change [HTML] view Plaincopyprint? <property name= "Age" update= "false" ></property> add @column to Property Get method in annotation (updatable=false)

[Java] View plaincopyprint?       @Column (updatable=false) public int getage () {return age; }


2. Using the Dynamic-update= "true" in XML

[HTML] View Plaincopyprint? <class name= "com.sccin.entity.Student" table= "Student" dynamic-update= "true" >

OK, so you don't need to set it on the field.

But this method is not in the annotation.


3. Use HQL statement (flexible, convenient) (recommended)

Modify the data using the HQL statement [Java] view plaincopyprint?           public void Update () {Sessions session = Hibernateuitl.getsessionfactory (). Getcurrentsession ();           Session.begintransaction ();           Query query = session.createquery ("Update Teacher t set t.name = ' YANGTIANB ' WHERE id = 3");           Query.executeupdate ();       Session.gettransaction (). commit (); }
4. SSH integration of the occasion

After loading the object that needs to be updated, use the Copyproperties method of the Beanutils class to copy the value that needs to be updated to the object, and then call the Update method. Note: The method recommended here is not the method in the Org.apache.comm*****.beanutils package, but the Copyproperties method in the Org.springframework.beans.BeanUtils. The reason is that the Spring tool class provides a copyproperties (source, Target, ignoreproperties) method that ignores the specified property values when copying object values, protects some values from malicious modification, and makes the object more secure. In addition, according to some test results the Copyproperties method in spring is more efficient than Apache (this is not further validated).
Reference code:
Admin persistent = adminservice.load (ID);//Load Object

Beanutils.copyproperties (admin, Persistent, new string[]{"id", "username"}),//Ignore ID, Username property when copying object property values, To avoid username being maliciously modified

Adminservice.update (persistent);//Update Object

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.