Search in sources :

Example 6 with Setter

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);
}
Also used : HashMap(java.util.HashMap) Setter(org.hibernate.property.access.spi.Setter) ClassLoaderServiceImpl(org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 7 with Setter

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;
}
Also used : Setter(org.hibernate.property.access.spi.Setter) XClass(org.hibernate.annotations.common.reflection.XClass)

Example 8 with Setter

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);
}
Also used : Setter(org.hibernate.property.access.spi.Setter)

Example 9 with Setter

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);
    }
}
Also used : Setter(org.hibernate.property.access.spi.Setter)

Example 10 with Setter

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);
}
Also used : Getter(org.hibernate.property.access.spi.Getter) Setter(org.hibernate.property.access.spi.Setter)

Aggregations

Setter (org.hibernate.property.access.spi.Setter)10 Getter (org.hibernate.property.access.spi.Getter)4 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AuditException (org.hibernate.envers.exception.AuditException)2 AnEntity (org.hibernate.serialization.entity.AnEntity)2 PK (org.hibernate.serialization.entity.PK)2 TestForIssue (org.hibernate.testing.TestForIssue)2 XClass (org.hibernate.annotations.common.reflection.XClass)1 ClassLoaderServiceImpl (org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl)1 PropertyData (org.hibernate.envers.internal.entities.PropertyData)1 GetterFieldImpl (org.hibernate.property.access.spi.GetterFieldImpl)1 GetterMethodImpl (org.hibernate.property.access.spi.GetterMethodImpl)1 SetterFieldImpl (org.hibernate.property.access.spi.SetterFieldImpl)1 SetterMethodImpl (org.hibernate.property.access.spi.SetterMethodImpl)1