Search in sources :

Example 21 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project CzechIdMng by bcvsolutions.

the class DefaultAuditService method getValuesFromVersion.

@Override
public Map<String, Object> getValuesFromVersion(Object revisionObject, List<String> auditedClass) {
    Map<String, Object> revisionValues = new HashMap<>();
    if (revisionObject == null) {
        return Collections.emptyMap();
    }
    // for better debug and readable there is no stream
    // we cannot use Introspector.getBeanInfo
    // getAllFieldsList get all field also with field from superclass
    List<Field> fields = FieldUtils.getAllFieldsList(revisionObject.getClass());
    for (Field field : fields) {
        try {
            // check if field has Audited annotation, or class
            if (!field.isAnnotationPresent(Audited.class) && !field.getDeclaringClass().isAnnotationPresent(Audited.class)) {
                continue;
            }
            // 
            PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(revisionObject, field.getName());
            // get property descriptor for readMethod
            if (propertyDescriptor == null) {
                continue;
            }
            // 
            Method readMethod = propertyDescriptor.getReadMethod();
            Object value = readMethod.invoke(revisionObject);
            // value can be null, but we want it
            if (value == null) {
                revisionValues.put(field.getName(), null);
                continue;
            }
            // 
            LazyInitializer hibernateLI = null;
            String className = null;
            if (value instanceof HibernateProxy) {
                HibernateProxy proxy = (HibernateProxy) value;
                hibernateLI = proxy.getHibernateLazyInitializer();
                className = hibernateLI.getEntityName();
            }
            // we have all audited class, then some not audited class (binding) and others primitive types
            if (className != null) {
                // get id from hibernate lazy initializer, entity may no longer exist, but ID in DB is always
                revisionValues.put(field.getName(), hibernateLI.getIdentifier());
            } else {
                revisionValues.put(field.getName(), value);
            }
        // 
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new ResultCodeException(CoreResultCode.BAD_REQUEST, ImmutableMap.of("field", field.getName()), e);
        }
    }
    return revisionValues;
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Method(java.lang.reflect.Method) HibernateProxy(org.hibernate.proxy.HibernateProxy) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field)

Example 22 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class ReadOnlyProxyTest method testReadOnlyViaLazyInitializerBeforeInit.

@Test
public void testReadOnlyViaLazyInitializerBeforeInit() {
    DataPoint dpOrig = createDataPoint(CacheMode.IGNORE);
    Session s = openSession();
    s.setCacheMode(CacheMode.IGNORE);
    s.beginTransaction();
    DataPoint dp = (DataPoint) s.load(DataPoint.class, new Long(dpOrig.getId()));
    assertTrue(dp instanceof HibernateProxy);
    LazyInitializer dpLI = ((HibernateProxy) dp).getHibernateLazyInitializer();
    assertTrue(dpLI.isUninitialized());
    checkReadOnly(s, dp, false);
    dpLI.setReadOnly(true);
    checkReadOnly(s, dp, true);
    dp.setDescription("changed");
    assertFalse(dpLI.isUninitialized());
    assertEquals("changed", dp.getDescription());
    checkReadOnly(s, dp, true);
    s.flush();
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    dp = (DataPoint) s.get(DataPoint.class, dpOrig.getId());
    assertEquals(dpOrig.getId(), dp.getId());
    assertEquals(dpOrig.getDescription(), dp.getDescription());
    assertEquals(dpOrig.getX(), dp.getX());
    assertEquals(dpOrig.getY(), dp.getY());
    s.delete(dp);
    s.getTransaction().commit();
    s.close();
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 23 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class StatefulPersistenceContext method reassociateProxy.

@Override
public void reassociateProxy(Object value, Serializable id) throws MappingException {
    if (value instanceof HibernateProxy) {
        LOG.debugf("Setting proxy identifier: %s", id);
        final HibernateProxy proxy = (HibernateProxy) value;
        final LazyInitializer li = proxy.getHibernateLazyInitializer();
        li.setIdentifier(id);
        reassociateProxy(li, proxy);
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 24 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class StatefulPersistenceContext method narrowProxy.

@Override
@SuppressWarnings("unchecked")
public Object narrowProxy(Object proxy, EntityPersister persister, EntityKey key, Object object) throws HibernateException {
    final Class concreteProxyClass = persister.getConcreteProxyClass();
    final boolean alreadyNarrow = concreteProxyClass.isInstance(proxy);
    if (!alreadyNarrow) {
        LOG.narrowingProxy(concreteProxyClass);
        // It would just be extra processing.  Just return the impl
        if (object != null) {
            proxiesByKey.remove(key);
            return object;
        }
        // Similarly, if the original HibernateProxy is initialized, there
        // is again no point in creating a proxy.  Just return the impl
        final HibernateProxy originalHibernateProxy = (HibernateProxy) proxy;
        if (!originalHibernateProxy.getHibernateLazyInitializer().isUninitialized()) {
            final Object impl = originalHibernateProxy.getHibernateLazyInitializer().getImplementation();
            // can we return it?
            if (concreteProxyClass.isInstance(impl)) {
                proxiesByKey.remove(key);
                return impl;
            }
        }
        // Otherwise, create the narrowed proxy
        final HibernateProxy narrowedProxy = (HibernateProxy) persister.createProxy(key.getIdentifier(), session);
        // set the read-only/modifiable mode in the new proxy to what it was in the original proxy
        final boolean readOnlyOrig = originalHibernateProxy.getHibernateLazyInitializer().isReadOnly();
        narrowedProxy.getHibernateLazyInitializer().setReadOnly(readOnlyOrig);
        return narrowedProxy;
    } else {
        if (object != null) {
            final LazyInitializer li = ((HibernateProxy) proxy).getHibernateLazyInitializer();
            li.setImplementation(object);
        }
        return proxy;
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 25 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project hibernate-orm by hibernate.

the class ReadOnlyCriteriaQueryTest method checkProxyReadOnly.

private void checkProxyReadOnly(Session s, Object proxy, boolean expectedReadOnly) {
    assertTrue(proxy instanceof HibernateProxy);
    LazyInitializer li = ((HibernateProxy) proxy).getHibernateLazyInitializer();
    assertSame(s, li.getSession());
    assertEquals(expectedReadOnly, s.isReadOnly(proxy));
    assertEquals(expectedReadOnly, li.isReadOnly());
    assertEquals(Hibernate.isInitialized(proxy), !li.isUninitialized());
    if (Hibernate.isInitialized(proxy)) {
        assertEquals(expectedReadOnly, s.isReadOnly(li.getImplementation()));
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Aggregations

HibernateProxy (org.hibernate.proxy.HibernateProxy)33 LazyInitializer (org.hibernate.proxy.LazyInitializer)33 Session (org.hibernate.Session)7 EntityEntry (org.hibernate.engine.spi.EntityEntry)7 Test (org.junit.Test)7 Serializable (java.io.Serializable)3 HibernateException (org.hibernate.HibernateException)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 MappingException (org.hibernate.MappingException)2 ObjectDeletedException (org.hibernate.ObjectDeletedException)2 PersistentObjectException (org.hibernate.PersistentObjectException)2 SessionImplementor (org.hibernate.engine.SessionImplementor)2 EntityKey (org.hibernate.engine.spi.EntityKey)2 EventSource (org.hibernate.event.spi.EventSource)2 UnknownSqlResultSetMappingException (org.hibernate.procedure.UnknownSqlResultSetMappingException)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1