hibernate 中更新某些字段的几种update方法
来源:互联网 发布:.me域名的市场 编辑:程序博客网 时间:2024/05/17 14:27
Hibernate 中如果直接使用
Session.update(Object o);
会把这个表中的所有字段更新一遍。
比如:
view plaincopy to clipboardprint?
public class TeacherTest {
}
public class TeacherTest {
@Test
public void update(){
}
}
Hibernate 执行的SQL语句:
view plaincopy to clipboardprint?
Hibernate:
Hibernate:
我们只更改了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)
@Column(updatable=false)
public int getAge() {
}
我们在执行 Update方法会发现,age 属性 不会被更改
view plaincopy to clipboardprint?
Hibernate:
Hibernate:
缺点:不灵活····
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(){
public void update(){
}
Hibernate 执行的SQL语句:
view plaincopy to clipboardprint?
Hibernate:
Hibernate:
这样就只更新了我们更新的字段······
- hibernate 中更新某些字段的几种update方法
- Hibernate更新某些字段的几种update方法
- Hibernate更新某些字段的几种update方法
- Hibernate更新某些字段的几种update方法
- Hibernate更新某些字段的几种update方法
- 关于更新部分属性,Hibernate更新某些字段的几种update方法
- hibernate update 只更新部分字段的3种方法
- hibernate update 只更新部分字段的3种方法
- hibernate update 只更新部分字段的3种方法
- hibernate update 只更新部分字段的3种方法
- UPDATE更新某些字段
- hibernate中的update方法只更新部分字段的方法
- Hibernate中更新数据的update方法
- Hibernate update 更新部分更改的字段
- Hibernate 更新部分更改的字段 hibernate update
- Hibernate 更新部分更改的字段 hibernate update
- Hibernate 更新部分更改的字段 hibernate update
- Hibernate 更新部分更改的字段 hibernate update
- 对话框扩展 OnCreate OnInitialDialog 控件焦点,按键回车变化
- hdu 3848 CC On The Tree
- hdu 1003
- C语言编译过程总结详解
- 程序2011.8.6
- hibernate 中更新某些字段的几种update方法
- 思想与命运的互动
- Sql优化文档
- SQL优化34条
- 为什么推荐使用谷歌加载jquery库文件
- hdu 1058 (DP)
- Altium Designer 设计心得
- SSH操作思路-我来帮助你
- 学习感想