问题描述
在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在INSERT
或UPDATE
之后通过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查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!