Search in sources :

Example 26 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class SessionImpl method bestGuessEntityName.

@Override
public String bestGuessEntityName(Object object) {
    if (object instanceof HibernateProxy) {
        LazyInitializer initializer = ((HibernateProxy) object).getHibernateLazyInitializer();
        // so make certain that we do not accidentally initialize an uninitialized proxy
        if (initializer.isUninitialized()) {
            return initializer.getEntityName();
        }
        object = initializer.getImplementation();
    }
    EntityEntry entry = persistenceContext.getEntry(object);
    if (entry == null) {
        return guessEntityName(object);
    } else {
        return entry.getPersister().getEntityName();
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 27 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class SessionImpl method getCurrentLockMode.

@Override
public LockMode getCurrentLockMode(Object object) throws HibernateException {
    checkOpen();
    checkTransactionSynchStatus();
    if (object == null) {
        throw new NullPointerException("null object passed to getCurrentLockMode()");
    }
    if (object instanceof HibernateProxy) {
        object = ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation(this);
        if (object == null) {
            return LockMode.NONE;
        }
    }
    EntityEntry e = persistenceContext.getEntry(object);
    if (e == null) {
        throw new TransientObjectException("Given object not associated with the session");
    }
    if (e.getStatus() != Status.MANAGED) {
        throw new ObjectDeletedException("The given object was deleted", e.getId(), e.getPersister().getEntityName());
    }
    return e.getLockMode();
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) EntityEntry(org.hibernate.engine.spi.EntityEntry) ObjectDeletedException(org.hibernate.ObjectDeletedException) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 28 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry 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 29 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class SessionImpl method contains.

@Override
public boolean contains(String entityName, Object object) {
    checkOpen();
    checkTransactionSynchStatus();
    if (object == null) {
        return false;
    }
    try {
        if (!HibernateProxy.class.isInstance(object) && persistenceContext.getEntry(object) == null) {
            // check if it is an entity -> if not throw an exception (per JPA)
            try {
                getSessionFactory().getMetamodel().entityPersister(entityName);
            } catch (HibernateException e) {
                throw new IllegalArgumentException("Not an entity [" + entityName + "] : " + object);
            }
        }
        if (object instanceof HibernateProxy) {
            //do not use proxiesByKey, since not all
            //proxies that point to this session's
            //instances are in that collection!
            LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
            if (li.isUninitialized()) {
                //the underlying instance will be "contained"
                return li.getSession() == this;
            } else {
                //if it is initialized, see if the underlying
                //instance is contained, since we need to
                //account for the fact that it might have been
                //evicted
                object = li.getImplementation();
            }
        }
        // A session is considered to contain an entity only if the entity has
        // an entry in the session's persistence context and the entry reports
        // that the entity has not been removed
        EntityEntry entry = persistenceContext.getEntry(object);
        delayedAfterCompletion();
        return entry != null && entry.getStatus() != Status.DELETED && entry.getStatus() != Status.GONE;
    } catch (MappingException e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e);
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateException(org.hibernate.HibernateException) HibernateProxy(org.hibernate.proxy.HibernateProxy) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException) MappingException(org.hibernate.MappingException)

Example 30 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class SessionImpl method contains.

@Override
public boolean contains(Object object) {
    checkOpen();
    checkTransactionSynchStatus();
    if (object == null) {
        return false;
    }
    try {
        if (object instanceof HibernateProxy) {
            //do not use proxiesByKey, since not all
            //proxies that point to this session's
            //instances are in that collection!
            LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
            if (li.isUninitialized()) {
                //the underlying instance will be "contained"
                return li.getSession() == this;
            } else {
                //if it is initialized, see if the underlying
                //instance is contained, since we need to
                //account for the fact that it might have been
                //evicted
                object = li.getImplementation();
            }
        }
        // A session is considered to contain an entity only if the entity has
        // an entry in the session's persistence context and the entry reports
        // that the entity has not been removed
        EntityEntry entry = persistenceContext.getEntry(object);
        delayedAfterCompletion();
        if (entry == null) {
            if (!HibernateProxy.class.isInstance(object) && persistenceContext.getEntry(object) == null) {
                // check if it is even an entity -> if not throw an exception (per JPA)
                try {
                    final String entityName = getEntityNameResolver().resolveEntityName(object);
                    if (entityName == null) {
                        throw new IllegalArgumentException("Could not resolve entity-name [" + object + "]");
                    }
                    getSessionFactory().getMetamodel().entityPersister(object.getClass());
                } catch (HibernateException e) {
                    throw new IllegalArgumentException("Not an entity [" + object.getClass() + "]", e);
                }
            }
            return false;
        } else {
            return entry.getStatus() != Status.DELETED && entry.getStatus() != Status.GONE;
        }
    } catch (MappingException e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e);
    }
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) HibernateException(org.hibernate.HibernateException) HibernateProxy(org.hibernate.proxy.HibernateProxy) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException) MappingException(org.hibernate.MappingException)

Aggregations

EntityEntry (org.hibernate.engine.spi.EntityEntry)79 Serializable (java.io.Serializable)22 EntityPersister (org.hibernate.persister.entity.EntityPersister)21 Session (org.hibernate.Session)18 Test (org.junit.Test)17 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)16 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)12 EntityKey (org.hibernate.engine.spi.EntityKey)12 TestForIssue (org.hibernate.testing.TestForIssue)12 AssertionFailure (org.hibernate.AssertionFailure)11 HibernateProxy (org.hibernate.proxy.HibernateProxy)11 HibernateException (org.hibernate.HibernateException)10 EventSource (org.hibernate.event.spi.EventSource)9 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)8 TransientObjectException (org.hibernate.TransientObjectException)7 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)7 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 Status (org.hibernate.engine.spi.Status)6 LazyInitializer (org.hibernate.proxy.LazyInitializer)6 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)5