Java Hibernate 使用条件执行更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25817997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:13:01  来源:igfitidea点击:

Hibernate execute update with criteria

javasqlhibernateorm

提问by vlio20

Is it possible to execute an update while using Criteriain Hibernate? For example:

使用Criteriain 时是否可以执行更新Hibernate?例如:

Session session = getSession();
Criteria crit = session.createCriteria(User.class);
crit.add(Restrictions.eq("token", sessionToken));

User user= new User();
Transaction tx = session.getTransaction();
try 
{
    tx.begin();
    session.updateWithCriteria(user, crit); //my imaginary function 
    tx.commit();
}
catch (Exception e) 
{
    e.printStackTrace();
    tx.rollback();
}

session.close();

采纳答案by Radim K?hler

There is a very powerful feature called:

有一个非常强大的功能叫做:

15.4. DML-style operations

15.4. DML 风格的操作

small cite from doc:

来自 doc 的小引用:

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

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

So, while this is not about criteria- we still can use our domain model for querying, because it is about HQL. This is a snippet showing the power:

所以,虽然这不是关于criteria- 我们仍然可以使用我们的域模型进行查询,因为它是关于HQL 的。这是一个显示力量的片段:

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 = s.createQuery( hqlUpdate )
        .setString( "newName", newName )
        .setString( "oldName", oldName )
        .executeUpdate();
tx.commit();
session.close();

SUMMARY: Having that in place:

总结:有了它:

  • we can use query to filter results
  • we can apply bulk update on it
  • we won't need to load these rows in memory, into the session...
  • 我们可以使用查询来过滤结果
  • 我们可以对其应用批量更新
  • 我们不需要将这些行加载到内存中,进入会话......

回答by ?smail Yavuz

First you should get the object then modify and update:

首先你应该得到对象然后修改和更新:

   Query q = session.createQuery("from StockTransaction where tranId = :tranId ");
   q.setParameter("tranId", 11);
   StockTransaction stockTran = (StockTransaction)q.list().get(0);

   stockTran.setVolume(4000000L);
   session.update(stockTran);

If you want to use partial/dynamic update feature then put

如果你想使用部分/动态更新功能,那么把

@org.hibernate.annotations.Entity(
        dynamicUpdate = true
)

annotation on top of the dao class.

dao 类顶部的注释。

Example from: http://www.mkyong.com/hibernate/hibernate-dynamic-update-attribute-example/

示例来自:http: //www.mkyong.com/hibernate/hibernate-dynamic-update-attribute-example/

Note: The Question is "with criteria" but the accepted answer is NOT "with criteria" but SQL.

注意:问题是“有条件”,但接受的答案不是“有条件”而是 SQL。

回答by Payal Bansal

Now we can do something like this for bulk update and delete. New api's released for criteriaUpdate and CriteriaDelete

现在我们可以为批量更新和删除做这样的事情。发布了用于标准更新和标准删除的新 api

CriteriaBuilder cb = this.em.getCriteriaBuilder();
// create update
CriteriaUpdate<Order> update = cb.createCriteriaUpdate(Order.class);
// set the root class
Root e = update.from(Order.class);
// set update and where clause
update.set("amount", newAmount);
update.where(cb.greaterThanOrEqualTo(e.get("amount"), oldAmount));
// perform update
this.em.createQuery(update).executeUpdate();