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)