首页 技术 正文
技术 2022年11月9日
0 收藏 457 点赞 5,018 浏览 2490 个字

hibernate update

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

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

下一篇: 配置sphinx
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,112
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,585
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922