Search in sources :

Example 31 with LazyInitializer

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

the class DefaultPersistEventListener method onPersist.

/**
 * Handle the given create event.
 *
 * @param event The create event to be handled.
 */
public void onPersist(PersistEvent event, Map createCache) throws HibernateException {
    final SessionImplementor source = event.getSession();
    final Object object = event.getObject();
    final Object entity;
    if (object instanceof HibernateProxy) {
        LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
        if (li.isUninitialized()) {
            if (li.getSession() == source) {
                // NOTE EARLY EXIT!
                return;
            } else {
                throw new PersistentObjectException("uninitialized proxy passed to persist()");
            }
        }
        entity = li.getImplementation();
    } else {
        entity = object;
    }
    final String entityName;
    if (event.getEntityName() != null) {
        entityName = event.getEntityName();
    } else {
        entityName = source.bestGuessEntityName(entity);
        event.setEntityName(entityName);
    }
    final EntityEntry entityEntry = source.getPersistenceContext().getEntry(entity);
    EntityState entityState = getEntityState(entity, entityName, entityEntry, source);
    if (entityState == EntityState.DETACHED) {
        // JPA 2, in its version of a "foreign generated", allows the id attribute value
        // to be manually set by the user, even though this manual value is irrelevant.
        // The issue is that this causes problems with the Hibernate unsaved-value strategy
        // which comes into play here in determining detached/transient state.
        // 
        // Detect if we have this situation and if so null out the id value and calculate the
        // entity state again.
        // NOTE: entityEntry must be null to get here, so we cannot use any of its values
        EntityPersister persister = source.getFactory().getEntityPersister(entityName);
        if (ForeignGenerator.class.isInstance(persister.getIdentifierGenerator())) {
            if (LOG.isDebugEnabled() && persister.getIdentifier(entity, source) != null) {
                LOG.debug("Resetting entity id attribute to null for foreign generator");
            }
            persister.setIdentifier(entity, null, source);
            entityState = getEntityState(entity, entityName, entityEntry, source);
        }
    }
    switch(entityState) {
        case DETACHED:
            {
                throw new PersistentObjectException("detached entity passed to persist: " + getLoggableName(event.getEntityName(), entity));
            }
        case PERSISTENT:
            {
                entityIsPersistent(event, createCache);
                break;
            }
        case TRANSIENT:
            {
                entityIsTransient(event, createCache);
                break;
            }
        case DELETED:
            {
                entityEntry.setStatus(Status.MANAGED);
                entityEntry.setDeletedState(null);
                event.getSession().getActionQueue().unScheduleDeletion(entityEntry, event.getObject());
                entityIsDeleted(event, createCache);
                break;
            }
        default:
            {
                throw new ObjectDeletedException("deleted entity passed to persist", null, getLoggableName(event.getEntityName(), entity));
            }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) ObjectDeletedException(org.hibernate.ObjectDeletedException) PersistentObjectException(org.hibernate.PersistentObjectException) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 32 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project ma-core-public by infiniteautomation.

the class H3BeanConverter method getClass.

/**
 * Hibernate makes {@link Class#getClass()} diffficult ...
 *
 * @param example
 *            The class that we want to call {@link Class#getClass()} on
 * @return The type of the given object
 */
public Class getClass(Object example) {
    if (example instanceof HibernateProxy) {
        HibernateProxy proxy = (HibernateProxy) example;
        LazyInitializer initializer = proxy.getHibernateLazyInitializer();
        SessionImplementor implementor = initializer.getSession();
        if (initializer.isUninitialized()) {
            try {
                // getImplementation is going to want to talk to a session
                if (implementor.isClosed()) {
                    // Give up and return example.getClass();
                    return example.getClass();
                }
            } catch (NoSuchMethodError ex) {
            // We must be using Hibernate 3.0/3.1 which doesn't have
            // this method
            }
        }
        return initializer.getImplementation().getClass();
    } else {
        return example.getClass();
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) SessionImplementor(org.hibernate.engine.SessionImplementor) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 33 with LazyInitializer

use of org.hibernate.proxy.LazyInitializer in project dropwizard by dropwizard.

the class AbstractDAOTest method initializesProxies.

@Test
void initializesProxies() throws Exception {
    final LazyInitializer initializer = mock(LazyInitializer.class);
    when(initializer.isUninitialized()).thenReturn(true);
    final HibernateProxy proxy = mock(HibernateProxy.class);
    when(proxy.getHibernateLazyInitializer()).thenReturn(initializer);
    dao.initialize(proxy);
    verify(initializer).initialize();
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) HibernateProxy(org.hibernate.proxy.HibernateProxy) Test(org.junit.jupiter.api.Test)

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