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:
- 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.
- 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?
How Hibernate constructs an SQL Update can be configured to update only the fields that changed, using an attribute called
dynamic-updateon 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: