Hibernate更新某些字段的几种update方法

简介: Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍。 比如: view plaincopy to clipboardprint? public class TeacherTest { @Test public void update(){ Session session = Hibern

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

目录
相关文章
|
Java 数据库连接 数据库
|
缓存 Java 数据库连接
|
Java 数据库连接 API
|
Java 数据库连接 应用服务中间件
|
存储 Oracle Java
[亲测可用]hibernate调用Oracle存储过程|Spring Data JPA调用Oracle存储过程方法
[亲测可用]hibernate调用Oracle存储过程|Spring Data JPA调用Oracle存储过程方法
|
存储 缓存 Java
Hibernate - Session方法与持久化对象详解
Hibernate - Session方法与持久化对象详解
495 0
|
SQL 缓存 Java
框架分析(9)-Hibernate
框架分析(9)-Hibernate
|
缓存 Java 数据库连接
Hibernate:Java持久层框架的高效应用
通过上述步骤,可以在Java项目中高效应用Hibernate框架,实现对关系数据库的透明持久化管理。Hibernate提供的强大功能和灵活配置,使得开发者能够专注于业务逻辑的实现,而不必过多关注底层数据库操作。
441 1
|
SQL Java 数据库连接
Hibernate 是一款开源 ORM(对象关系映射)框架,封装了 JDBC,允许以面向对象的方式操作数据库,简化了数据访问层的开发。
Hibernate 是一款开源 ORM(对象关系映射)框架,封装了 JDBC,允许以面向对象的方式操作数据库,简化了数据访问层的开发。通过映射机制,它可以自动处理对象与数据库表之间的转换,支持主流数据库,提高了代码的可移植性和可维护性。其核心接口包括 SessionFactory、Session 和 Transaction 等,通过它们可以执行数据库的 CRUD 操作。配置方面,需在项目中引入 Hibernate 及数据库驱动依赖,并创建 `hibernate.cfg.xml` 配置文件来设置数据库连接和 Hibernate 行为参数。
491 1