简体   繁体   English

hibernate hql - 执行更新查询后返回更新的行 ID 列表

[英]hibernate hql - return updated rows id list after execute update query

I'm using Hibernate Query Language(HQL) with Oracle database in my Java spring MVC application.我在我的 Java spring MVC 应用程序中使用 Hibernate Query Language(HQL) 和 Oracle 数据库。 I have write an HQL update query in this way:我以这种方式编写了一个 HQL 更新查询:

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

The executeUpdate() method returns number of updated rows. executeUpdate()方法返回更新的行数。 But I want to get a list of the ids of updated rows after executing the update query.但我想在执行更新查询后获取更新行的 id 列表。 Is there any way of doing this in HQL?有没有办法在 HQL 中做到这一点?

As far as I know there is no such functionality in JPA/Hibernate.据我所知,JPA/Hibernate 中没有这样的功能。 But you can create native query and use native SQL.但是您可以创建本机查询并使用本机 SQL。 I do not know oracle, but in PostgreSQL I would write :我不知道 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();

May be oracle have similar functional and this will help you.可能 oracle 有类似的功能,这会对你有所帮助。

Blaze-Persistence, a library that works on top of JPA/Hibernate, adds support for this. Blaze-Persistence 是一个在 JPA/Hibernate 之上工作的库,增加了对此的支持。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM