use of org.hibernate.property.access.spi.Setter in project hibernate-orm by hibernate.
the class MapProxyTest method shouldGenerateClassWithAppropriateSetter.
@Test
public void shouldGenerateClassWithAppropriateSetter() throws Exception {
//given
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Class<?>> properties = new HashMap<String, Class<?>>();
properties.put("age", Integer.class);
//when
Class testClass = MapProxyTool.classForName("TestClass2", properties, new ClassLoaderServiceImpl());
Object testClassInstance = testClass.getConstructor(Map.class).newInstance(map);
//then
Setter setter = ReflectionTools.getSetter(testClass, "age", "property", serviceRegistry);
int ageExpected = 14;
setter.set(testClassInstance, ageExpected, null);
Object age = map.get("age");
Assert.assertEquals(ageExpected, age);
}
use of org.hibernate.property.access.spi.Setter in project hibernate-orm by hibernate.
the class ReflectionTools method getSetter.
public static Setter getSetter(Class cls, String propertyName, String accessorType, ServiceRegistry serviceRegistry) {
final Pair<Class, String> key = Pair.make(cls, propertyName);
Setter value = SETTER_CACHE.get(key);
if (value == null) {
value = getAccessStrategy(cls, serviceRegistry, accessorType).buildPropertyAccess(cls, propertyName).getSetter();
// It's ok if two setters are generated concurrently
SETTER_CACHE.put(key, value);
}
return value;
}
use of org.hibernate.property.access.spi.Setter in project hibernate-orm by hibernate.
the class AbstractToOneMapper method setPropertyValue.
protected void setPropertyValue(Object targetObject, Object value) {
final Setter setter = ReflectionTools.getSetter(targetObject.getClass(), propertyData, serviceRegistry);
setter.set(targetObject, value, null);
}
use of org.hibernate.property.access.spi.Setter in project hibernate-orm by hibernate.
the class SinglePropertyMapper method mapToEntityFromMap.
@Override
public void mapToEntityFromMap(EnversService enversService, Object obj, Map data, Object primaryKey, AuditReaderImplementor versionsReader, Number revision) {
// synthetic properties are not part of the entity model; therefore they should be ignored.
if (data == null || obj == null || propertyData.isSynthetic()) {
return;
}
final Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyData, enversService.getServiceRegistry());
final Object value = data.get(propertyData.getName());
// We only set a null value if the field is not primite. Otherwise, we leave it intact.
if (value != null || !isPrimitive(setter, propertyData, obj.getClass())) {
setter.set(obj, value, null);
}
}
use of org.hibernate.property.access.spi.Setter in project hibernate-orm by hibernate.
the class SingleIdMapper method mapToEntityFromEntity.
public void mapToEntityFromEntity(Object objTo, Object objFrom) {
if (objTo == null || objFrom == null) {
return;
}
final Getter getter = ReflectionTools.getGetter(objFrom.getClass(), propertyData, getServiceRegistry());
final Setter setter = ReflectionTools.getSetter(objTo.getClass(), propertyData, getServiceRegistry());
setter.set(objTo, getter.get(objFrom), null);
}
Aggregations