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

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

hibernate update部分更新的更多相关文章

  1. hibernate update 只更新部分字段的3种方法(转载)

    hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: public class Teacher Test { @Test p ...

  2. hibernate update 只更新部分字段的3种方法(其实我只想说第二种)

    hibernate 中如果直接使用Session.update(Object o);会把这个表中的所有字段更新一遍. 比如: public class Teacher Test { @Test pub ...

  3. Hibernate之即时更新

    昨天工作中遇到了一个简单的问题,弄了好久,都怪自己没有好好的去了解hibernate,导致了这样的问题弄了两三个小时. 问题是这样的:我想即时更改数据,然后再查询 (1)用Spring的getHibe ...

  4. 利用带关联子查询Update语句更新数据

    Update是T-sql中再简单不过的语句了,update table set column=expression  [where condition],我们都会用到.但update的用法不仅于此,真 ...

  5. Windows 8.1 & Windows 10 取消 Windows Update 自动更新硬件驱动

    最新文章:Virson's Blog 1.打开控制面板,在搜索框中搜索“设备”一次,检索出相关的设备设置功能,如下图: 2.在检索出的结果中点击“更改设备安装设置”,会弹出设备驱动的更新方式,按照如下 ...

  6. mssql sql高效关联子查询的update 批量更新

    /* 使用带关联子查询的Update更新     --1.创建测试表 create TABLE Table1     (     a varchar(10),     b varchar(10),   ...

  7. 屏幕旋转时调用PopupWindow update方法更新位置失效的问题及解决方案

       接到一个博友的反馈,在屏幕旋转时调用PopupWindow的update方法失效.使用场景如下:在一个Activity中监听屏幕旋转事件,在Activity主布局文件中有个按钮点击弹出一个Pop ...

  8. .NET CORE 实践(3)--Visual Studio 2015 Update 3更新之后DotNetCore.1.0.1-VS2015Tools.Preview2.0.2.exe无法正确安装

    打开 https://www.microsoft.com/net/core#windows,点击 https://go.microsoft.com/fwlink/?LinkId=691129下载vs2 ...

  9. 为什么SQL用UPDATE语句更新时更新行数会多3行有触发器有触发器有触发器有触发器有触发器有触发器

    update明显更新就一行,但是结果显示更新多行. 原因是有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器

随机推荐

  1. php给一张图片加上水印效果

    <?php /** * 功能:给一张图片加上水印效果 * $i 要加水印效果的图片 * $t 水印文字 * $size 文字大小 * $pos 水印的位置 * $color 文字的颜色 * $f ...

  2. ACM ICPC Team

    Link: https://www.hackerrank.com/challenges/acm-icpc-team/submissions/code/11617807 def count_max_to ...

  3. 继续畅通工程--hdu1879

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  4. [bzoj1002][FJOI2007 轮状病毒] (生成树计数+递推+高精度)

    Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下图 ...

  5. 【CCNA学习笔记】1.思科路由器的基本配置

    教学视频来源:http://edu.51cto.com/lesson/id-10930.html. 怎么安装模块.连交叉线什么的视频里面老师说的很清楚了,我只记录一下IOS配置的命令(虽然一副不明觉厉 ...

  6. Linux iostat监测IO状态(转)

    Linux iostat监测IO状态 2010-03-1  |  13:13分类:Linux,技术细节  |  标签:Linux  |  53,945 views Linux系统出现了性能问题,一般我 ...

  7. keil C 应注意的几个问题

    我们使用Keil C调试某系统时积累的一些经验 1.在Windows2000下面,我们可以把字体设置为Courier,这样就可以显示正常.2.当使用有片外内存的MCU(如W77E58,它有1K片外内存 ...

  8. Redhat Enterprise Linux中如何关闭SELinux?

    转自http://www.cnitblog.com/lywaml/archive/2005/06/21/468.html 红帽企业 Linux 4 包括了一个 SELinux 的实现.SELinux ...

  9. 【转】linux tree命令以树形结构显示文件目录结构 ---- 不错

    原文网址:http://jingyan.baidu.com/article/acf728fd19c7eff8e510a3eb.html 今天小编来给分享Linux 系统下一个非常有用的命令的使用:tr ...

  10. 【HDU1272】小希的迷宫(并查集基础题)

    仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...