Learner
Learner

Reputation: 21393

update attribute for property element in hibernate

The property element of Hibernate has attributes update, insert and as per documentation it states that - http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/mapping.html#mapping-declaration-property

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.

Please help me in understanding how it behaves if its value is set as 'false' instead of 'true'. What is the derived property here?

Upvotes: 2

Views: 946

Answers (1)

alterfox
alterfox

Reputation: 1695

You can see the difference in an example - if you create an entity and persist it using default insert settings, all fields that the entity has set will be persisted, and you will see in Hibernate log the expected generated insert SQL statement. Something like:

Hibernate: insert into SCHEMA1.User (id, firstName, lastName) values (default, ?, ?)

But if you set insert for some property to false, even if the field has value in the entity, the generated SQL will have the appropriate column excluded, so the value for it won't be persisted. If the firstName has insert set to false, the generated SQL is:

Hibernate: insert into SCHEMA1.User (id, lastName) values (default, ?)

This will work if firstName is nullable, and fail otherwise.


The described principle also applies to update.


Setting these attributes to false makes sense in the case mentioned in docs, where you deal with a derived (calculated) property that you do not persist. It is in fact a read-only property with value computed at fetch time (computation is declared as an SQL expression which translates to a SELECT subquery). You can check this post for an example and links for more information about derived properties.

Upvotes: 1

Related Questions