hibernate hql-执行更新查询后返回更新的行ID列表 [英] hibernate hql - return updated rows id list after execute update query
问题描述
我在Java spring MVC应用程序中将Hibernate Query Language(HQL)与Oracle数据库一起使用.我以这种方式编写了一个HQL更新查询:
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();
executeUpdate()
方法返回更新的行数.但是我想在执行更新查询后获取更新行的ID的列表. HQL有什么方法可以做到这一点?
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?
推荐答案
据我所知,JPA/Hibernate中没有此类功能.但是您可以创建本机查询并使用本机SQL.我不知道oracle,但是在PostgreSQL中我会写:
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();
可能是oracle具有类似的功能,这将对您有所帮助.
May be oracle have similar functional and this will help you.
这篇关于hibernate hql-执行更新查询后返回更新的行ID列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!