session对 PO的生命周期管理object =session.load(..... object.setxx.... session.flush(); out sql: update ... set xx=.... where 但在PO的生命周期之外就不行了还是用我开发的持久层吧,集成了对属性的版本管理资料地址http://wwww.jnsbc.com:6868/catframework.zip
Hibernate 中如果直接使用
Session.update(Object o);
会把这个表中的所有字段更新一遍。
比如: 1 2 3 4 5 6 7 8 9 10 11 12 public class TeacherTest { @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(); } }
Hibernate 执行的SQL语句:
1 2 3 4 5 6 7 8 9 10 Hibernate: UPDATE Teacher SET age=?, birthday=?, name=?, title=? WHERE id=?
呵呵,不知道是不是你想要的答案~annotation是否有此功能就不清楚了...
@Entity
@org.hibernate.annotations.Entity(selectBeforeUpdate = true, dynamicInsert = true, dynamicUpdate = true)
@Table(name = "AUTHORIZE")
@BatchSize(size = 20)
public class Authorize extends BaseModel {
object.setxx....
session.flush();
out sql: update ... set xx=.... where
但在PO的生命周期之外就不行了还是用我开发的持久层吧,集成了对属性的版本管理资料地址http://wwww.jnsbc.com:6868/catframework.zip
Session.update(Object o);
会把这个表中的所有字段更新一遍。
比如:
1
2
3
4
5
6
7
8
9
10
11
12 public class TeacherTest {
@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();
}
}
Hibernate 执行的SQL语句:
1
2
3
4
5
6
7
8
9
10 Hibernate:
UPDATE
Teacher
SET
age=?,
birthday=?,
name=?,
title=?
WHERE
id=?
我们只更改了Name属性,而Hibernate 的sql语句 把所有字段都更改了一次。
这样要是我们有字段是文本类型,这个类型存储的内容是几千,几万字,这样效率会很低。
那么怎么只更改我们更新的字段呢?
有三中方法:
1.XML中设置property 标签 update = “false” ,如下:我们设置 age 这个属性在更改中不做更改
<property name=”age” update=”false”></property>
在Annotation中 在属性GET方法上加上@Column(updatable=false)
1
2
3
4 @Column(updatable=false)
public int getAge() {
return age;
}
我们在执行 Update方法会发现,age 属性 不会被更改
1
2
3
4
5
6
7
8
9 Hibernate:
UPDATE
Teacher
SET
birthday=?,
name=?,
title=?
WHERE
id=?
缺点:不灵活••••
2.第2种方法••使用XML中的 dynamic-update=”true”
<class name="com.sccin.entity.Student" table="student" dynamic-update="true">
OK,这样就不需要在字段上设置了。
但这样的方法在Annotation中没有
3.第三种方式:使用HQL语句(灵活,方便)
使用HQL语句修改数据
1
2
3
4
5
6
7 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();
}
Hibernate 执行的SQL语句:
Hibernate:
update
Teacher
set
name='yangtianb'
where
id=3
这样就只更新了我们更新的字段••••••