Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an UPDATE RETURNING query in Hibernate

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

Is there similar functionality in Hibernate?

like image 224
hypno7oad Avatar asked Nov 14 '22 08:11

hypno7oad


1 Answers

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.

like image 149
tscho Avatar answered Nov 16 '22 02:11

tscho