Search in sources :

Example 11 with Getter

use of org.hibernate.property.access.spi.Getter in project hibernate-orm by hibernate.

the class MapProxyTest method shouldGenerateClassWithAppropriateGetter.

@Test
public void shouldGenerateClassWithAppropriateGetter() throws Exception {
    //given
    Map<String, Object> map = new HashMap<String, Object>();
    int ageExpected = 14;
    map.put("age", ageExpected);
    Map<String, Class<?>> properties = new HashMap<String, Class<?>>();
    properties.put("age", Integer.class);
    //when
    Class testClass = MapProxyTool.classForName("TestClass1", properties, new ClassLoaderServiceImpl());
    Object testClassInstance = testClass.getConstructor(Map.class).newInstance(map);
    //then
    Getter getter = ReflectionTools.getGetter(testClass, "age", "property", serviceRegistry);
    int age = (Integer) getter.get(testClassInstance);
    Assert.assertEquals(ageExpected, age);
}
Also used : HashMap(java.util.HashMap) Getter(org.hibernate.property.access.spi.Getter) ClassLoaderServiceImpl(org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 12 with Getter

use of org.hibernate.property.access.spi.Getter in project hibernate-orm by hibernate.

the class ReflectionTools method getGetter.

public static Getter getGetter(Class cls, String propertyName, String accessorType, ServiceRegistry serviceRegistry) {
    final Pair<Class, String> key = Pair.make(cls, propertyName);
    Getter value = GETTER_CACHE.get(key);
    if (value == null) {
        value = getAccessStrategy(cls, serviceRegistry, accessorType).buildPropertyAccess(cls, propertyName).getGetter();
        // It's ok if two getters are generated concurrently
        GETTER_CACHE.put(key, value);
    }
    return value;
}
Also used : Getter(org.hibernate.property.access.spi.Getter) XClass(org.hibernate.annotations.common.reflection.XClass)

Example 13 with Getter

use of org.hibernate.property.access.spi.Getter in project hibernate-orm by hibernate.

the class EnversServiceImpl method initializeAuditStrategy.

private static AuditStrategy initializeAuditStrategy(String auditStrategyName, Class<?> revisionInfoClass, PropertyData revisionInfoTimestampData, ServiceRegistry serviceRegistry) {
    AuditStrategy strategy;
    try {
        final Class<?> auditStrategyClass = loadClass(auditStrategyName, serviceRegistry);
        strategy = (AuditStrategy) ReflectHelper.getDefaultConstructor(auditStrategyClass).newInstance();
    } catch (Exception e) {
        throw new MappingException(String.format("Unable to create AuditStrategy [%s] instance.", auditStrategyName), e);
    }
    if (strategy instanceof ValidityAuditStrategy) {
        // further initialization required
        final Getter revisionTimestampGetter = ReflectionTools.getGetter(revisionInfoClass, revisionInfoTimestampData, serviceRegistry);
        ((ValidityAuditStrategy) strategy).setRevisionTimestampGetter(revisionTimestampGetter);
    }
    return strategy;
}
Also used : Getter(org.hibernate.property.access.spi.Getter) AuditStrategy(org.hibernate.envers.strategy.AuditStrategy) ValidityAuditStrategy(org.hibernate.envers.strategy.ValidityAuditStrategy) ValidityAuditStrategy(org.hibernate.envers.strategy.ValidityAuditStrategy) MappingException(org.hibernate.MappingException) MappingException(org.hibernate.MappingException)

Example 14 with Getter

use of org.hibernate.property.access.spi.Getter in project hibernate-orm by hibernate.

the class SingleIdMapper method mapToMapFromEntity.

@Override
public void mapToMapFromEntity(Map<String, Object> data, Object obj) {
    if (obj == null) {
        data.put(propertyData.getName(), null);
    } else {
        if (obj instanceof HibernateProxy) {
            final HibernateProxy hibernateProxy = (HibernateProxy) obj;
            data.put(propertyData.getName(), hibernateProxy.getHibernateLazyInitializer().getIdentifier());
        } else {
            final Getter getter = ReflectionTools.getGetter(obj.getClass(), propertyData, getServiceRegistry());
            data.put(propertyData.getName(), getter.get(obj));
        }
    }
}
Also used : Getter(org.hibernate.property.access.spi.Getter) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 15 with Getter

use of org.hibernate.property.access.spi.Getter 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

Getter (org.hibernate.property.access.spi.Getter)15 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 Map (java.util.Map)4 Setter (org.hibernate.property.access.spi.Setter)4 AnEntity (org.hibernate.serialization.entity.AnEntity)4 PK (org.hibernate.serialization.entity.PK)4 TestForIssue (org.hibernate.testing.TestForIssue)4 HashMap (java.util.HashMap)2 ClassLoaderServiceImpl (org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl)2 PropertyData (org.hibernate.envers.internal.entities.PropertyData)2 GetterFieldImpl (org.hibernate.property.access.spi.GetterFieldImpl)2 GetterMethodImpl (org.hibernate.property.access.spi.GetterMethodImpl)2 FlushModeType (javax.persistence.FlushModeType)1 LockModeType (javax.persistence.LockModeType)1 TemporalType (javax.persistence.TemporalType)1 MappingException (org.hibernate.MappingException)1