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

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

最新文章

  1. 如何安装虚拟机VMware
  2. equals和“==”
  3. 纸上谈兵:伸展树(splay tree)
  4. GPT分区基础知识
  5. 如何用BMFont制作图片字
  6. UVa 10420 List of Conquests
  7. C++ 中判断非空的错误指针
  8. 单独启动tomcat
  9. WCF Test Client
  10. UML和模式应用学习笔记-1(面向对象分析和设计)
  11. 监控mysql主从
  12. TensorFlow学习笔记3——变量共享
  13. 变量类型、sprintf、不同类型之间的混合运算
  14. 3dmax导入模型,解决贴图不显示的问题
  15. mysql无法远程连接到数据库解决方法
  16. 【T10】记住,TCP__IP不是轮询的
  17. Python 实例变量
  18. JAVA 本地序列化。
  19. Android改进版CoverFlow效果控件
  20. MySQL Replication(Master与Slave基本原理及配置)

热门文章

  1. DataTable 导出Excel 下载 (NPOI)
  2. [bzoj 1001][Beijing2006]狼抓兔子 (最小割+对偶图+最短路)
  3. 让WordPress的作者在后台只能看到自己的文章
  4. CloudStack API访问权限控制
  5. HDU 4010 Query on The Trees(动态树)
  6. openwrt下和云端通讯的例程,
  7. thinkpad t530 centos 6.4 有线网卡 设置
  8. C 本地文件夸网文件Cp操作
  9. Fancy
  10. C++小知识之sprintf用法