HQL - Update field and return resulting value in one query HQL - Update field and return resulting value in one query postgresql postgresql

HQL - Update field and return resulting value in one query


There isn't really anything in HQL to replace the returning clause in SQL. Executing UPDATE statements in a query in Hibernate is usually avoided. It breaks the caches, alters entity fields without rolling the version number, hoses the in memory state of your session, and is generally bad juju. Normally in a hibernate application you'd only use an update query for large batch operations where the performance consequences of using the Session normally are just too much.

If you're doing this with high enough frequency that locking the entity prior to the update is an issue, the native sql probably is your best option. I wouldn't personally worry about it until it's proven to be a problem. You're not really doing that much extra work in the DB cursor to select for update the things you're about to update, but I am not a postgres expert either.