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

陌路散爱 提交于 2020-07-18 05:12:11

问题


I'm using Hibernate Query Language(HQL) with Oracle database in my Java spring MVC application. I have write an HQL update query in this way:

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. But I want to get a list of the ids of updated rows after executing the update query. Is there any way of doing this in HQL?


回答1:


As far as I know there is no such functionality in JPA/Hibernate. But you can create native query and use native SQL. I do not know oracle, but in PostgreSQL I would write :

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.




回答2:


Blaze-Persistence, a library that works on top of JPA/Hibernate, adds support for this.

Here some more information about that: https://persistence.blazebit.com/documentation/core/manual/en_US/index.html#returning-from-update-statement



来源:https://stackoverflow.com/questions/47987328/hibernate-hql-return-updated-rows-id-list-after-execute-update-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!