Search in sources :

Example 41 with HibernateProxy

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

the class SessionImpl method getIdentifier.

// not for internal use:
@Override
public Serializable getIdentifier(Object object) throws HibernateException {
    checkOpen();
    checkTransactionSynchStatus();
    if (object instanceof HibernateProxy) {
        LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
        if (li.getSession() != this) {
            throw new TransientObjectException("The proxy was not associated with this session");
        }
        return li.getIdentifier();
    } else {
        EntityEntry entry = persistenceContext.getEntry(object);
        if (entry == null) {
            throw new TransientObjectException("The instance was not associated with this session");
        }
        return entry.getId();
    }
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 42 with HibernateProxy

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

the class ByteBuddyProxyFactory method getProxy.

@Override
public HibernateProxy getProxy(Serializable id, SharedSessionContractImplementor session) throws HibernateException {
    final ByteBuddyInterceptor interceptor = new ByteBuddyInterceptor(entityName, persistentClass, interfaces, id, getIdentifierMethod, setIdentifierMethod, componentIdType, session, overridesEquals);
    try {
        final HibernateProxy proxy = (HibernateProxy) proxyClass.newInstance();
        ((ProxyConfiguration) proxy).$$_hibernate_set_interceptor(interceptor);
        return proxy;
    } catch (Throwable t) {
        LOG.error(LOG.bytecodeEnhancementFailed(entityName), t);
        throw new HibernateException(LOG.bytecodeEnhancementFailed(entityName), t);
    }
}
Also used : ProxyConfiguration(org.hibernate.proxy.ProxyConfiguration) HibernateException(org.hibernate.HibernateException) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 43 with HibernateProxy

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

the class ByteBuddyProxyFactory method deserializeProxy.

public static HibernateProxy deserializeProxy(SerializableProxy serializableProxy) {
    final ByteBuddyInterceptor interceptor = new ByteBuddyInterceptor(serializableProxy.getEntityName(), serializableProxy.getPersistentClass(), serializableProxy.getInterfaces(), serializableProxy.getId(), resolveIdGetterMethod(serializableProxy), resolveIdSetterMethod(serializableProxy), serializableProxy.getComponentIdType(), null, ReflectHelper.overridesEquals(serializableProxy.getPersistentClass()));
    // note: interface is assumed to already contain HibernateProxy.class
    try {
        final Class proxyClass = buildProxy(serializableProxy.getPersistentClass(), serializableProxy.getInterfaces());
        final HibernateProxy proxy = (HibernateProxy) proxyClass.newInstance();
        ((ProxyConfiguration) proxy).$$_hibernate_set_interceptor(interceptor);
        return proxy;
    } catch (Throwable t) {
        final String message = LOG.bytecodeEnhancementFailed(serializableProxy.getEntityName());
        LOG.error(message, t);
        throw new HibernateException(message, t);
    }
}
Also used : ProxyConfiguration(org.hibernate.proxy.ProxyConfiguration) HibernateException(org.hibernate.HibernateException) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 44 with HibernateProxy

use of org.hibernate.proxy.HibernateProxy in project webofneeds by researchstudio-sat.

the class ParentAwareFlushEventListener method onFlushEntity.

@Override
public void onFlushEntity(final FlushEntityEvent event) throws HibernateException {
    final EntityEntry entry = event.getEntityEntry();
    final Object entity = event.getEntity();
    final boolean mightBeDirty = entry.requiresDirtyCheck(entity);
    if (mightBeDirty && entity instanceof ParentAware) {
        ParentAware parentAware = (ParentAware) entity;
        if (updated(event)) {
            VersionedEntity parent = parentAware.getParent();
            if (parent == null)
                return;
            if (logger.isDebugEnabled()) {
                logger.debug("Incrementing {} entity version because a {} child entity has been updated", parent, entity);
            }
            if (!(parent instanceof HibernateProxy)) {
                // we have to do the increment manually
                parent.incrementVersion();
            }
            Hibernate.initialize(parent);
            event.getSession().save(parent);
        } else if (deleted(event)) {
            VersionedEntity parent = parentAware.getParent();
            if (parent == null)
                return;
            if (logger.isDebugEnabled()) {
                logger.debug("Incrementing {} entity version because a {} child entity has been deleted", root, entity);
            }
            if (!(parent instanceof HibernateProxy)) {
                // we have to do the increment manually
                parent.incrementVersion();
            }
            Hibernate.initialize(parent);
            event.getSession().save(parent);
        }
    }
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 45 with HibernateProxy

use of org.hibernate.proxy.HibernateProxy 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)

Aggregations

HibernateProxy (org.hibernate.proxy.HibernateProxy)130 Session (org.hibernate.Session)58 Test (org.junit.Test)56 LazyInitializer (org.hibernate.proxy.LazyInitializer)33 DefaultPostLoaderDao (org.broadleafcommerce.common.persistence.DefaultPostLoaderDao)16 PostLoaderDao (org.broadleafcommerce.common.persistence.PostLoaderDao)16 EntityEntry (org.hibernate.engine.spi.EntityEntry)13 Serializable (java.io.Serializable)11 Transaction (org.hibernate.Transaction)10 TransientObjectException (org.hibernate.TransientObjectException)10 EntityPersister (org.hibernate.persister.entity.EntityPersister)10 HibernateException (org.hibernate.HibernateException)8 AdminPresentationMergeOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride)5 EntityKey (org.hibernate.engine.spi.EntityKey)4 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 Iterator (java.util.Iterator)3 ObjectDeletedException (org.hibernate.ObjectDeletedException)3 EventSource (org.hibernate.event.spi.EventSource)3 BigDecimal (java.math.BigDecimal)2