Hibernate中update方法的注意点
来源:互联网 发布:pk10网站源码 编辑:程序博客网 时间:2024/05/17 23:06
Hibernate 中如果直接使用
Session.update(Object o);
会把这个表中的所有字段更新一遍。
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语句:
Hibernate:
update
Teacher
set
age=?,
birthday=?,
name=?,
title=?
where
id=?
我们只更改了Name属性,而Hibernate 的sql语句 把所有字段都更改了一次。
这样要是我们有字段是文本类型,这个类型存储的内容是几千,几万字,这样效率会很低。
那么怎么只更改我们更新的字段呢?
有三中方法:
1.XML中设置property 标签 update = “false” ,如下:我们设置 age 这个属性在更改中不做更改
<property name=”age” update=”false”></property>
<property name=”age” update=”false”></property>
在Annotation中 在属性GET方法上加上@Column(updatable=false)
@Column(updatable=false)
public int getAge() {
return age;
}
@Column(updatable=false)
public int getAge() {
return age;
}
我们在执行 Update方法会发现,age 属性 不会被更改
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语句:
Hibernate:
update
Teacher
set
name=’yangtianb’
where
id=3
Hibernate:
update
Teacher
set
name=’yangtianb’
where
id=3
这样就只更新了我们更新的字段······
转载请标明出处:http://blog.csdn.net/Baykermr/archive/2010/03/09/5359913.aspx
- Hibernate中update方法的注意点
- 有关hibernate中update方法的问题
- Hibernate中更新数据的update方法
- Java 中split 方法的注意点
- Hibernate 的一些注意点
- Hibernate 的一些注意点
- hibernate的使用注意点
- 浅谈Hibernate中HQL查询需要注意的几点
- hibernate 中更新某些字段的几种update方法
- 在hibernate中save() saveOrUpdate() update() meger方法的讲解
- hibernate中hql语句注意点
- Hibernate中使用自定义insert,update,delete语句需要注意的问题
- Hibernate中Update的使用
- java中方法override和overload的几个注意点
- ios开发中类方法以及self的注意点
- ios开发中类方法以及self的注意点
- Object-c ios7中方法定义的注意点
- Activity中onNewIntent()方法使用需要注意的点
- CentOS7 配置 vsftpd
- CHROME源码剖析 上《转》
- swift 使用MAGearRefreshControl控件刷新
- 关于死锁
- 背景音乐实现功能
- Hibernate中update方法的注意点
- android EditText默认问题
- win8.1下无法运行vc++6.0的解决方法
- 关于CentOS网络连接问题及第三天实习收获
- python中的input函数和raw_input函数的区别
- 二叉树的前序、中序、后序、层序遍历
- Kaprekar数
- Integer Partition(hdu4658)2013 Multi-University Training Contest 6 整数拆分二
- MySQL存储引擎总结