本文介绍了在Hibernate中创建UPDATE RETURNING查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Oracle中,我们可以创建一个更新查询,该查询将使用RETURNING子句返回更新的记录.

Hibernate中是否有类似的功能?

解决方案

除了数据库生成的值外,显然Hibernate也不需要返回更新后的实例,因为传递给Session.saveOrUpdate() 的对象是更新的实例.如果对数据库生成的值(顺序,触发器,默认值等)进行了相应注释(或在XML映射文件中定义),则将在 Session.saveOrUpdate之后设置它们.

对于标识符值与JPA @javax.persistence.Id注释结合使用JPA @javax.persistence.GeneratedValue注释.对于简单属性使用本地的Hibernate @org.hibernate.annotations.Generated注释(afaik没有相应的JPA注释).

Hibernate如何检索生成的标识符值取决于生成策略和/或数据库方言.对于简单属性,Hibernate在INSERTUPDATE之后通过id语句执行附加的SELECT.

In Oracle we can create an update query that will return the updated record using the RETURNING clause.

Is there similar functionality in Hibernate?

解决方案

Apart from database-generated values there is obviously no need for Hibernate to return the updated instance because the object passed to Session.saveOrUpdate() is the updated instance. Database-generated values (sequence, trigger, defaults, etc.) will be set after Session.saveOrUpdate if they are accordingly annotated (or defined in a XML mapping file).

For identifier values use the JPA @javax.persistence.GeneratedValue annotation in conjunction with the JPA @javax.persistence.Id annotation. For simple properties use the native Hibernate @org.hibernate.annotations.Generated annotation (afaik there is no according JPA annotation).

How generated identifier values are retrieved by Hibernate depends on the generation strategy and/or the database dialect. For simple properties Hibernate executes an additional SELECT by id statement after the INSERT or UPDATE.

这篇关于在Hibernate中创建UPDATE RETURNING查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:09