hibernate使用标准更新单列 [英] hibernate update single column using criteria

查看:111
本文介绍了hibernate使用标准更新单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含mamy列的表,我想更新行的一列或几列而不影响剩余的列
我可以写查询:

  update table as t set ta =:a set tb =:b where t.id = 1 

但看到我不知道哪些列将被选择更新,并且我认为为每个场景编写每个查询都不是一个好主意。那么,我必须为每个场景编写查询,但我正在寻找更好的方式来动态更新表。我想标准将是一个不错的选择。但问题是我不知道如何编写条件更新特定列。我的代码现在可以更新列,但它会将其他列设置为空或空。



在不更改其他列的情况下更新特定列的好方法是什么?

解决方案

Hibernate支持两种如何更新表格列的基本方法。

首先很自然,通过将实体加载到 session 中,在运行时更改它,flush (udpate)更改回数据库。这是标准,ORM风格。第二部分主要面向非常高效的SQL UPDATE 语句。这里记录为:

15.4。 DML风格的操作



来自doc:


...但是,Hibernate提供了通过Hibernate查询语言执行的批量SQL风格的DML语句执行的方法...

它不为标准查询提供API,但它可以与我们的域模型一起使用HQL ==。

我们可以在我们映射的实体上创建一个WHERE子句,并要求只更新我们选择的几列。 有一些限制(不支持JOIN),但它们可以通过子查询来解决......



这是doc中的一个片段: / p>

  Session session = sessionFactory.openSession(); 
Transaction tx = session.beginTransaction();

String hqlUpdate =update Customer c set c.name =:newName where c.name =:oldName;
//或字符串hqlUpdate =更新客户设置名称=:newName其中name =:oldName;
int updatedEntities = session.createQuery(hqlUpdate)
.setString(newName,newName)
.setString(oldName,oldName)
.executeUpdate();
tx.commit();
session.close();

另请查看本问与答: Hibernate用条件执行更新


I have a table that contains mamy columns and I want to update the one or few columns of the row without effecting remaining columns I can write query:

update table as t set t.a=:a set t.b=:b where t.id=1

But seen I dont know which columns will be selected to update, and I think it is not a good idea to write every query for every scenarios. Well, I have to write query for every scenarios, but I am looking for a better way to update the table dynamically. I am thinking criteria would be a good choice. But the problem is that I have no idea how to write criteria update specific column. My code now can update the column but it would set other column to null or empty.

What would be the good way to update specific columns without changing other columns?

解决方案

Hibernate supports two basic ways how to update tables' columns.

The first is natural, via loading entity into session, changing it in run-time, flush (udpate) the changes back to DB. This is a standard, ORM style.

The second is mostly oriented on very efficient SQL UPDATE statement. It is documented here as:

15.4. DML-style operations

cite from doc:

... However, Hibernate provides methods for bulk SQL-style DML statement execution that is performed through the Hibernate Query Language...

It does not provide API for criteria query, but it does work with HQL == with our domain model.

We can create a WHERE clause on top of our mapped entities, and ask for update only of a few columns, we selected. There are some limitations (JOIN is not supported) but they can be solved by subqueries...

This is a snippet from doc:

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName";
// or String hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = session.createQuery( hqlUpdate )
        .setString( "newName", newName )
        .setString( "oldName", oldName )
        .executeUpdate();
tx.commit();
session.close();

Also check this Q&Q: Hibernate execute update with criteria

这篇关于hibernate使用标准更新单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆