Search in sources :

Example 1 with Setter

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);
        }
    }
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData) Setter(org.hibernate.property.access.spi.Setter) AuditException(org.hibernate.envers.exception.AuditException) HashMap(java.util.HashMap) Map(java.util.Map) AuditException(org.hibernate.envers.exception.AuditException)

Example 2 with Setter

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

Example 3 with Setter

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

Example 4 with Setter

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) Setter(org.hibernate.property.access.spi.Setter) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) ObjectOutputStream(java.io.ObjectOutputStream) SetterMethodImpl(org.hibernate.property.access.spi.SetterMethodImpl) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with Setter

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));
}
Also used : GetterFieldImpl(org.hibernate.property.access.spi.GetterFieldImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) Setter(org.hibernate.property.access.spi.Setter) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) SetterFieldImpl(org.hibernate.property.access.spi.SetterFieldImpl) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

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