Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial update of Hibernate Model

Tags:

java

hibernate

What is the best practice for updating a model where only certain fields are meant to be updated?

Eg.

If I have a person model with:

Name Birthdate Address

And a form where I want to update only:

Address

The two options I can currently see are:

  1. To have a custom form model that has only address - on postback call the DB to retrieve my object to fill in Name and Birthdate, then persist back to the DB.
  2. Custom update SQL to only update the relevant fields.

I'd prefer to make use of the hibernate model if possible, so I'm using #1 currently.

Is there a better way?

like image 326
Donald Avatar asked Nov 14 '11 17:11

Donald


1 Answers

How Hibernate constructs an SQL Update can be configured to update only the fields that changed, using an attribute called dynamic-update on the class mapping.

If hibernate leaves useful-looking functionality like this deactivated by default it's because there are trade-offs to be made. The linked documentation goes on to say:

The dynamic-update and dynamic-insert settings are not inherited by subclasses, so they can also be specified on the or elements. Although these settings can increase performance in some cases, they can actually decrease performance in others.

like image 125
Nathan Hughes Avatar answered Nov 13 '22 17:11

Nathan Hughes