【问题标题】:hibernate hql - return updated rows id list after execute update queryhibernate hql - 执行更新查询后返回更新的行 ID 列表
【发布时间】:2018-06-07 19:29:41
【问题描述】:

我在我的 Java Spring MVC 应用程序中使用 Hibernate Query Language (HQL) 和 Oracle 数据库。我以这种方式编写了 HQL 更新查询:

String hql = "update " + MyModel.class.getName() + " e set e.field = " + value + " where ..."
//Acquiring session
...
Query query = session.createQuery(hql);
int result = query.executeUpdate();

executeUpdate() 方法返回更新的行数。但我想在执行更新查询后获取更新行的 id 列表。在 HQL 中有没有办法做到这一点?

【问题讨论】:

    标签: java oracle hibernate hql


    【解决方案1】:

    据我所知,JPA/Hibernate 中没有这样的功能。但是您可以创建本机查询并使用本机 SQL。我不知道 oracle,但在 PostgreSQL 中我会写:

    String sql = "update table set field = :values where ... returning id";
    Query query = session.createNativeQuery(sql);
    query.setParameter("value", value);
    List ids = query.list();
    

    可能是 oracle 具有类似的功能,这将对您有所帮助。

    【讨论】:

    【解决方案2】:

    在 JPA/Hibernate 之上运行的库 Blaze-Persistence 添加了对此的支持。

    这里有更多相关信息:https://persistence.blazebit.com/documentation/core/manual/en_US/index.html#returning-from-update-statement

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-24
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      相关资源
      最近更新 更多