Hibernate 的update语句性能详解

本文探讨了在 Hibernate 框架中使用 Session.update 方法进行更新操作时的问题,特别是当只更新部分字段时,系统会默认更新所有字段导致效率低下。文章提供了三种优化方法:通过 XML 配置设置 property 标签的 update 属性为 false,或者在 Annotation 中使用 @Column(updatable=false),以及使用 HQL 语句精确更新所需字段,以提高性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Hibernate 中如果直接使用

Session.update(Object o);

会把这个表中的所有字段更新一遍。

比如:

view plaincopy to clipboardprint? 
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();  
}  
} 
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语句:

view plaincopy to clipboardprint? 
Hibernate:  
update  
Teacher  
set  
age=?,  
birthday=?,  
name=?,  
title=?  
where  
id=? 
Hibernate: 
update 
Teacher 
set 
age=?, 
birthday=?, 
name=?, 
title=? 
where 
id=?

 

我们只更改了Name属性,而Hibernate 的sql语句 把所有字段都更改了一次。

这样要是我们有字段是文本类型,这个类型存储的内容是几千,几万字,这样效率会很低。

那么怎么只更改我们更新的字段呢?

有三中方法:

1.XML中设置property 标签 update = "false" ,如下:我们设置 age 这个属性在更改中不做更改

view plaincopy to clipboardprint? 
<property name="age" update="false"></property> 
<property name="age" update="false"></property>

在Annotation中 在属性GET方法上加上@Column(updatable=false)

view plaincopy to clipboardprint? 
@Column(updatable=false)  
public int getAge() {  
return age;  
} 
@Column(updatable=false) 
public int getAge() { 
return age; 
}

我们在执行 Update方法会发现,age 属性 不会被更改

view plaincopy to clipboardprint? 
Hibernate:  
update  
Teacher  
set  
birthday=?,  
name=?,  
title=?  
where  
id=? 
Hibernate: 
update 
Teacher 
set 
birthday=?, 
name=?, 
title=? 
where 
id=?

 

缺点:不灵活····

2.第2种方法··使用XML中的 dynamic-update="true"

view plaincopy to clipboardprint? 
<class name="com.sccin.entity.Student" table="student" dynamic-update="true"> 
<class name="com.sccin.entity.Student" table="student" dynamic-update="true">

 

OK,这样就不需要在字段上设置了。

但这样的方法在Annotation中没有

3.第三种方式:使用HQL语句(灵活,方便)

使用HQL语句修改数据

view plaincopy to clipboardprint? 
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();  
} 
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语句:

view plaincopy to clipboardprint? 
Hibernate:  
update  
Teacher  
set  
name='yangtianb'  
where  
id=3 
Hibernate: 
update 
Teacher 
set 
name='yangtianb' 
where 
id=3

 

这样就只更新了我们更新的字段······

 

自己整理的一些方法:

1、使用hidden标签,把不需要的字段也查出来,然后在返回去。

2、再查一边数据库,声明一个新的对象,再把页面传过来的数据set进去,保存这个新声明的对象

转载于:https://www.cnblogs.com/zengda/p/4423129.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值