How to avoid overwriting non-zero values ​​with zero values?

I use spring MVC to get JSON from the client and automatically create an object from it. The problem is that the client does not send to the server all the fields that are in the entity, but some fields are null and overwrite existing values ​​that cause userDao.persist (user). For example, I have this object:

@Entity public class User { @Id @GeneratedValue private int id; private String username; private String password; private String email; 

But the user never sends me a password, so an object built from JSON has the password field empty. I do not want the password field to be overwritten with a null value. There you can say that the sleep mode "if you find a zero value, ignore it and do not overwrite the value that is stored in the database?". I can not believe that this simple problem does not exist.

+4
source share
5 answers

If your problem is just a database, I suggest you use a stored procedure that checks if this value is zero, and then the dose does not change the existing value. That way, you can still send a null value, and your check will be on the server side, which is more reliable.

-1
source

I think the source of your problem is that the object you get from JSON parsing never had actual values ​​in it. This is a bean that only has the values ​​set in your JSON.

You need to load your entity from the database, and then set non-zero fields from your JSON to the loaded object. This way only fields that are delivered in JSON will be set.

I recommend an adapter of some type to “merge” (not a JPA merge) the database version and the JSON version before saving the database version.

Adding the @NotNull and bean constraints Validation will ensure that when you try to save the values ​​are non-zero. Unfortunately, they will not help you get the values ​​in the entity to save.

+6
source

I have the same problem. I solved it in this way .

 import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import java.lang.reflect.Field; import java.util.Hashtable; public class Updater { private final static Logger log = LogManager.getLogger(Updater.class); public static <E> E updater(E oldEntity, E newEntity) { Field[] newEntityFields = newEntity.getClass().getDeclaredFields(); Hashtable newHT = fieldsToHT(newEntityFields, newEntity); Class oldEntityClass = oldEntity.getClass(); Field[] oldEntityFields = oldEntityClass.getDeclaredFields(); for (Field field : oldEntityFields){ field.setAccessible(true); Object o = newHT.get(field.getName()); if (o != null){ try { Field f = oldEntityClass.getDeclaredField(field.getName()); f.setAccessible(true); log.info("setting " + f.getName()); f.set(oldEntity, o); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } } } return oldEntity; } private static Hashtable<String, Object> fieldsToHT(Field[] fields, Object obj){ Hashtable<String,Object> hashtable = new Hashtable<>(); for (Field field: fields){ field.setAccessible(true); try { Object retrievedObject = field.get(obj); if (retrievedObject != null){ log.info("scanning " + field.getName()); hashtable.put(field.getName(), field.get(obj)); } } catch (IllegalAccessException e) { e.printStackTrace(); } } return hashtable; } } 

This is clearly a workaround, but it seems to work smoothly ... in the following days, I think I will write the recursive part.

+2
source

Implement the settings for your attributes and perform validations.

0
source

Check out the Hibernate Validation project , which you can use to validate your object at the DAO level, as well as the Spring web level .

0
source

All Articles