hibernate update部分更新
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部分更新的更多相关文章
- hibernate update 只更新部分字段的3种方法(转载)
hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: public class Teacher Test { @Test p ...
- hibernate update 只更新部分字段的3种方法(其实我只想说第二种)
hibernate 中如果直接使用Session.update(Object o);会把这个表中的所有字段更新一遍. 比如: public class Teacher Test { @Test pub ...
- Hibernate之即时更新
昨天工作中遇到了一个简单的问题,弄了好久,都怪自己没有好好的去了解hibernate,导致了这样的问题弄了两三个小时. 问题是这样的:我想即时更改数据,然后再查询 (1)用Spring的getHibe ...
- 利用带关联子查询Update语句更新数据
Update是T-sql中再简单不过的语句了,update table set column=expression [where condition],我们都会用到.但update的用法不仅于此,真 ...
- Windows 8.1 & Windows 10 取消 Windows Update 自动更新硬件驱动
最新文章:Virson's Blog 1.打开控制面板,在搜索框中搜索“设备”一次,检索出相关的设备设置功能,如下图: 2.在检索出的结果中点击“更改设备安装设置”,会弹出设备驱动的更新方式,按照如下 ...
- mssql sql高效关联子查询的update 批量更新
/* 使用带关联子查询的Update更新 --1.创建测试表 create TABLE Table1 ( a varchar(10), b varchar(10), ...
- 屏幕旋转时调用PopupWindow update方法更新位置失效的问题及解决方案
接到一个博友的反馈,在屏幕旋转时调用PopupWindow的update方法失效.使用场景如下:在一个Activity中监听屏幕旋转事件,在Activity主布局文件中有个按钮点击弹出一个Pop ...
- .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 ...
- 为什么SQL用UPDATE语句更新时更新行数会多3行有触发器有触发器有触发器有触发器有触发器有触发器
update明显更新就一行,但是结果显示更新多行. 原因是有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器
随机推荐
- 手机user agent大全下载 整理发布一批移动设备的user agent【分享】
手机user agent大全下载 整理发布一批移动设备的user agent[分享] 很多人朋友在玩浏览器的时候 或者写软件的时候需要用到 user agent 这个东西 修改这个 可以使自己的浏览器 ...
- c# 菜单无限极分类-利用递归
表结构: 前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Web ...
- [TYVJ] P1423 GF和猫咪的玩具
GF和猫咪的玩具 描述 Description GF同学和猫咪得到了一个特别的玩具,这个玩具由n个金属环(编号为1---n),和m条绳索组成,每条绳索连接两个不同的金属环,并且长度相同.GF左手拿起金 ...
- Oracle多行记录合并处理
1:效果如下图所示: 表T1: CREATE TABLE T1 ( WEEKWORKID VARCHAR2(20) , DD VARCHAR2(20) ) 表T2 CREATE TABLE T2 ( ...
- Xs and Os Referee
Xs and Os Referee 1 def checkio(game_result): 2 winner = 'D' 3 4 for row in game_result: 5 if row[0] ...
- 2015第15周日PostgreSQL学习
英文版官网地址:http://www.postgresql.org/ 上面显示的最新版本信息是PostgreSQL 9.4.1, 9.3.6, 9.2.10, 9.1.15 & 9.0.19 ...
- 【czy系列赛】czy的后宫4 && bzoj1925 [Sdoi2010]地精部落
[问题描述] czy有很多妹子,妹子虽然数量很多,但是质量不容乐观,她们的美丽值全部为负数(喜闻乐见). czy每天都要带N个妹子到机房,她们都有一个独一无二的美丽值,美丽值为-1到-N之间的整数.他 ...
- 【POJ2196】Specialized Four-Digit Numbers(暴力打表)
一道水题,只要会复制粘贴就好! #include <iostream> #include <cstring> #include <cstdlib> #include ...
- pyqt QTimer,QThread例子学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from PyQ ...
- RelativeLayout经常使用属性介绍
以下介绍一下RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false android:layout_centerHrizontal ...