use of org.hibernate.property.access.spi.Setter in project hibernate-orm by hibernate.
the class ComponentPropertyMapper method mapToEntityFromMap.
@Override
public void mapToEntityFromMap(EnversService enversService, Object obj, Map data, Object primaryKey, AuditReaderImplementor versionsReader, Number revision) {
if (data == null || obj == null) {
return;
}
if (propertyData.getBeanName() == null) {
// If properties are not encapsulated in a component but placed directly in a class
// (e.g. by applying <properties> tag).
delegate.mapToEntityFromMap(enversService, obj, data, primaryKey, versionsReader, revision);
return;
}
final Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyData, enversService.getServiceRegistry());
// If all properties are null and single, then the component has to be null also.
boolean allNullAndSingle = true;
for (Map.Entry<PropertyData, PropertyMapper> property : delegate.getProperties().entrySet()) {
if (data.get(property.getKey().getName()) != null || !(property.getValue() instanceof SinglePropertyMapper)) {
allNullAndSingle = false;
break;
}
}
if (allNullAndSingle) {
// single property, but default value need not be null, so we'll set it to null anyway
setter.set(obj, null, null);
} else {
// set the component
try {
final Object subObj = ReflectHelper.getDefaultConstructor(componentClass).newInstance();
setter.set(obj, subObj, null);
delegate.mapToEntityFromMap(enversService, subObj, data, primaryKey, versionsReader, revision);
} catch (Exception e) {
throw new AuditException(e);
}
}
}
use of org.hibernate.property.access.spi.Setter in project hibernate-orm by hibernate.
the class EmbeddedIdMapper method mapToEntityFromMap.
@Override
public boolean mapToEntityFromMap(Object obj, Map data) {
if (data == null || obj == null) {
return false;
}
final Getter getter = ReflectionTools.getGetter(obj.getClass(), idPropertyData, getServiceRegistry());
final Setter setter = ReflectionTools.getSetter(obj.getClass(), idPropertyData, getServiceRegistry());
try {
final Object subObj = ReflectHelper.getDefaultConstructor(getter.getReturnType()).newInstance();
boolean ret = true;
for (IdMapper idMapper : ids.values()) {
ret &= idMapper.mapToEntityFromMap(subObj, data);
}
if (ret) {
setter.set(obj, subObj, null);
}
return ret;
} catch (Exception e) {
throw new AuditException(e);
}
}
use of org.hibernate.property.access.spi.Setter in project hibernate-orm by hibernate.
the class SingleIdMapper method mapToEntityFromMap.
@Override
public boolean mapToEntityFromMap(Object obj, Map data) {
if (data == null || obj == null) {
return false;
}
final Object value = data.get(propertyData.getName());
if (value == null) {
return false;
}
final Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyData, getServiceRegistry());
setter.set(obj, value, null);
return true;
}
use of org.hibernate.property.access.spi.Setter in project hibernate-orm by hibernate.
the class GetterSetterSerializationTest method testProtectedMethodSetter.
@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testProtectedMethodSetter() throws Exception {
final AnEntity entity = new AnEntity(new PK(1L));
final Getter getter = new GetterMethodImpl(AnEntity.class, "pk", ReflectHelper.findGetterMethod(AnEntity.class, "pk"));
final Setter setter = new SetterMethodImpl(AnEntity.class, "pk", ReflectHelper.findSetterMethod(AnEntity.class, "pk", PK.class));
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(setter);
final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
final Setter setterClone = (Setter) ois.readObject();
final PK pkNew = new PK(2L);
setterClone.set(entity, pkNew, null);
assertSame(pkNew, getter.get(entity));
}
use of org.hibernate.property.access.spi.Setter in project hibernate-orm by hibernate.
the class GetterSetterSerializationTest method testPrivateFieldSetter.
@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testPrivateFieldSetter() throws Exception {
AnEntity entity = new AnEntity(new PK(1L));
final String propertyName = "pk";
final Getter getter = new GetterFieldImpl(AnEntity.class, propertyName, ReflectHelper.findField(AnEntity.class, propertyName));
final Setter setter = new SetterFieldImpl(AnEntity.class, propertyName, ReflectHelper.findField(AnEntity.class, propertyName));
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(setter);
final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
final Setter setterClone = (Setter) ois.readObject();
final PK pkNew = new PK(2L);
setterClone.set(entity, pkNew, null);
assertSame(pkNew, getter.get(entity));
}
Aggregations