Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partially filled entity objects and keeping data coherent during updates

I have a query that only fetches part of a table's values into an entity-style object. These values are being manipulated in a graphical interface.

The issue is that an update could overwrite any unused values that have been modified instead of leaving them untouched.

What is the proper way to deal with this? Should I write a method that does a partial update or simply create a new entity object for the purposes of editing?

like image 210
James P. Avatar asked Dec 21 '25 00:12

James P.


1 Answers

I'm curious as well on how this is best handled. In some projects, where I could make assumptions such as "null values in the bean mean the value has not been touched", I wrote the UPDATE statement to "update" all fields, setting the column value to that of the bean if not null, else setting the column value to the pre-existing column value in the db.

UPDATE entity
  SET propertyA = NVL(newPropertyA, propertyA)
     ,propertyB = NVL(newPropertyB, propertyB)
     ,propertyC = NVL(newPropertyC, propertyC)
     ,...
like image 80
Glenn Avatar answered Dec 23 '25 15:12

Glenn