Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting update and insert property in Hibernate

In the hibernate HBM file, what is the purpose of setting the following properties?

update = "false"
insert = "false"

What purpose do they serve? What difference do they make to the performance? When should we use them?

like image 470
Chillax Avatar asked Mar 09 '12 18:03

Chillax


2 Answers

Use update="false", insert="false" when the property is calculated/derived, or when the database or triggers are responsible for inserting/ or updating the value.

For example, if the DB would automatically generate a value on INSERT that you want to use, then specify insert="false" so Hibernate won't include the property in INSERT statements.

Another example, would be a calculated/derived property via SQL formula: You could, for example, retrieve a sum of order-totals for each customer. Eg:

<property name="totalOrders" insert="false" update="false">
  <formula>(select sum(ORDER.TOTAL) from ORDER where ORDER.FK_CUSTOMER=ID)</formula>
</property>

In this case we set insert="false", update="false" since this is clearly a derived result, and we can't update it directly.

Performance? It's not about performance -- it's about what your database mapping requires.

like image 174
Thomas W Avatar answered Sep 22 '22 17:09

Thomas W


From the documentation:

update, insert (optional - defaults to true): specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" property whose value is initialized from some other property that maps to the same column(s), or by a trigger or other application.

like image 43
JB Nizet Avatar answered Sep 25 '22 17:09

JB Nizet