Hibernate 更新部分更改的字段 hibernate update
2010-05-22 20:38
337 查看
Hibernate 中如果直接使用
Session.update(Object o);
会把这个表中的所有字段更新一遍。
比如:
view plaincopy to clipboardprint?
01.public class TeacherTest {
02. @Test
03. public void update(){
04. Session session = HibernateUitl.getSessionFactory().getCurrentSession();
05. session.beginTransaction();
06. Teacher t = (Teacher) session.get(Teacher.class, 3);
07. t.setName("yangtb2");
08. session.update(t);
09.
10. session.getTransaction().commit();
11. }
12.}
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?
01.Hibernate:
02. update
03. Teacher
04. set
05. age=?,
06. birthday=?,
07. name=?,
08. title=?
09. where
10. id=?
Hibernate:
update
Teacher
set
age=?,
birthday=?,
name=?,
title=?
where
id=?
我们只更改了Name属性,而Hibernate 的sql语句 把所有字段都更改了一次。
这样要是我们有字段是文本类型,这个类型存储的内容是几千,几万字,这样效率会很低。
那么怎么只更改我们更新的字段呢?
有三中方法:
1.XML中设置property 标签 update = "false" ,如下:我们设置 age 这个属性在更改中不做更改
view plaincopy to clipboardprint?
01.<property name="age" update="false"></property>
<property name="age" update="false"></property>
在Annotation中 在属性GET方法上加上@Column(updatable=false)
view plaincopy to clipboardprint?
01.@Column(updatable=false)
02. public int getAge() {
03. return age;
04. }
@Column(updatable=false)
public int getAge() {
return age;
}
我们在执行 Update方法会发现,age 属性 不会被更改
view plaincopy to clipboardprint?
01.Hibernate:
02. update
03. Teacher
04. set
05. birthday=?,
06. name=?,
07. title=?
08. where
09. id=?
Hibernate:
update
Teacher
set
birthday=?,
name=?,
title=?
where
id=?
缺点:不灵活····
2.第2种方法··使用XML中的 dynamic-update="true"
view plaincopy to clipboardprint?
01.<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语句修改数据
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Baykermr/archive/2010/03/09/5359913.aspx
Session.update(Object o);
会把这个表中的所有字段更新一遍。
比如:
view plaincopy to clipboardprint?
01.public class TeacherTest {
02. @Test
03. public void update(){
04. Session session = HibernateUitl.getSessionFactory().getCurrentSession();
05. session.beginTransaction();
06. Teacher t = (Teacher) session.get(Teacher.class, 3);
07. t.setName("yangtb2");
08. session.update(t);
09.
10. session.getTransaction().commit();
11. }
12.}
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?
01.Hibernate:
02. update
03. Teacher
04. set
05. age=?,
06. birthday=?,
07. name=?,
08. title=?
09. where
10. id=?
Hibernate:
update
Teacher
set
age=?,
birthday=?,
name=?,
title=?
where
id=?
我们只更改了Name属性,而Hibernate 的sql语句 把所有字段都更改了一次。
这样要是我们有字段是文本类型,这个类型存储的内容是几千,几万字,这样效率会很低。
那么怎么只更改我们更新的字段呢?
有三中方法:
1.XML中设置property 标签 update = "false" ,如下:我们设置 age 这个属性在更改中不做更改
view plaincopy to clipboardprint?
01.<property name="age" update="false"></property>
<property name="age" update="false"></property>
在Annotation中 在属性GET方法上加上@Column(updatable=false)
view plaincopy to clipboardprint?
01.@Column(updatable=false)
02. public int getAge() {
03. return age;
04. }
@Column(updatable=false)
public int getAge() {
return age;
}
我们在执行 Update方法会发现,age 属性 不会被更改
view plaincopy to clipboardprint?
01.Hibernate:
02. update
03. Teacher
04. set
05. birthday=?,
06. name=?,
07. title=?
08. where
09. id=?
Hibernate:
update
Teacher
set
birthday=?,
name=?,
title=?
where
id=?
缺点:不灵活····
2.第2种方法··使用XML中的 dynamic-update="true"
view plaincopy to clipboardprint?
01.<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语句修改数据
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Baykermr/archive/2010/03/09/5359913.aspx
相关文章推荐
- Hibernate 更新部分更改的字段 hibernate update
- Hibernate——Hibernate4.2更新部分更改的字段 hibernate update
- Hibernate 更新部分更改的字段 hibernate update
- Hibernate 更新部分更改的字段 hibernate update
- Hibernate 更新部分更改的字段 hibernate update
- hibernate update 只更新部分字段的3种方法(转载)
- Hibernate4 更新部分更改的字段 hibernate update
- Hibernate之update及更新部分字段
- Hibernate 更新部分更改的字段 hibernate update
- 关于更新部分属性,Hibernate更新某些字段的几种update方法
- hibernate中的update方法只更新部分字段的方法
- hibernate update 只更新部分字段的3种方法
- hibernate update 只更新部分字段的3种方法
- SSH利用getHibernateTemplate().update更新所需要更新的部分字段
- hibernate update 只更新部分字段的3种方法(转载)
- Hibernate update 仅更新部分字段的实现方式
- hibernate中update只更新部分字段,避免二次提交数据策略
- Hibernate 更新部分更改的字段 hibernate update
- Hibernate之update(1)——更新部分字段
- Hibernate 更新部分更改的字段 hibernate update