イントロスペクションを使用して、マーシャリングされていないオブジェクトをデータベース内の既存のオブジェクトとマージできます。
import java.beans.Introspector;
...
Class<?> entityClass = YourEntity.class
// oldEntity is from database
oldEntity = entityManager.getReference(entityclass, id);
for (PropertyDescriptor property : Introspector.getBeanInfo(entityClass).getPropertyDescriptors()) {
if (property.getReadMethod() != null && property.getWriteMethod() != null) {
// You retrieve the value of the current property of your unmarshalled entity
Object newValue = property.getReadMethod().invoke(unmarshalledEntity);
if (value != null) {
property.getWriteMethod().invoke(oldEntity, newValue);
}
}
}
ただし、このソリューションでは、フィールドを次のように更新することはできませんnull
...問題になる可能性があります。そして、これは単なるアイデアです。タイプに注意する必要がvalue
あります。別のBeanである可能性があるため、データを再帰的にイントロスペクトする必要があります。