Hibernate update only updates some of the 3 methods of a field (in fact, I just want to say the second kind)

Source: Internet
Author: User

Hibernate if you are using direct
Session.update (Object o);
All the fields in this table will be updated again.

Like what:

public class Teacher Test {    @Test public    Void Update () {         Session session = Hibernateuitl.getsessionfactory () . Getcurrentsession ();        Session.begintransaction ();        Teacher t = (Teacher) session.get (teacher.class,3);        T.setname ("Yangtb2");        Session.update (t);        Session.gettransaction (). commit ();    }}

SQL statement executed by Hibernate:
代码

Hibernate:updateteacher setage=?,birthday=?,name=?,title=? Whereid=?

We only changed the Name property, and Hibernate's SQL statement changed all fields once.
So if we have a field that is text type, this type of storage is thousands of, tens of thousands of words, so it will be very inefficient.
So how do we change only the fields we've updated?
There are three ways to do this:

Set Property Tag update = "false" in 1.XML, as follows: We set the age attribute to change without changing



Add @column (Updatable=false) to the property Get method in annotation

@Column (Updatable=false)
public int getage () {
return age;
}

When we execute the Update method, we find that the age property is not changed

Hibernate:
UPDATE
Teacher
SET
Birthday=?,
Name=?,
Title=?
WHERE
Id=?

Cons: Not flexible

2.2nd Method • Using dynamic-update= "true" in XML

<classname= "Com.sccin.entity.Student" table= "Student" dynamic-update= "true"/>

OK, so you don't need to set it on the field.
But such a method is not in the annotation.

3. Third Way: Use HQL statement (flexible, convenient)

modifying data using the HQL statement
public void Update () {
Session 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 ();
}

SQL statement executed by Hibernate:
Hibernate:
Update
Teacher
Set
Name= ' Yangtianb '
where
Id=3

This only updates our updated fields

Hibernate update only updates some of the 3 methods of a field (in fact, I just want to say the second kind)

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.