Search in sources :

Example 31 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class StandardCacheEntryImpl method assemble.

/**
	 * Assemble the previously disassembled state represented by this entry into the given entity instance.
	 *
	 * Additionally manages the PreLoadEvent callbacks.
	 *
	 * @param instance The entity instance
	 * @param id The entity identifier
	 * @param persister The entity persister
	 * @param interceptor (currently unused)
	 * @param session The session
	 *
	 * @return The assembled state
	 *
	 * @throws HibernateException Indicates a problem performing assembly or calling the PreLoadEventListeners.
	 *
	 * @see org.hibernate.type.Type#assemble
	 * @see org.hibernate.type.Type#disassemble
	 */
public Object[] assemble(final Object instance, final Serializable id, final EntityPersister persister, final Interceptor interceptor, final EventSource session) throws HibernateException {
    if (!persister.getEntityName().equals(subclass)) {
        throw new AssertionFailure("Tried to assemble a different subclass instance");
    }
    //assembled state gets put in a new array (we read from cache by value!)
    final Object[] assembledProps = TypeHelper.assemble(disassembledState, persister.getPropertyTypes(), session, instance);
    //persister.setIdentifier(instance, id); //beforeQuery calling interceptor, for consistency with normal load
    //TODO: reuse the PreLoadEvent
    final PreLoadEvent preLoadEvent = new PreLoadEvent(session).setEntity(instance).setState(assembledProps).setId(id).setPersister(persister);
    final EventListenerGroup<PreLoadEventListener> listenerGroup = session.getFactory().getServiceRegistry().getService(EventListenerRegistry.class).getEventListenerGroup(EventType.PRE_LOAD);
    for (PreLoadEventListener listener : listenerGroup.listeners()) {
        listener.onPreLoad(preLoadEvent);
    }
    persister.setPropertyValues(instance, assembledProps);
    return assembledProps;
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) PreLoadEventListener(org.hibernate.event.spi.PreLoadEventListener) PreLoadEvent(org.hibernate.event.spi.PreLoadEvent) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 32 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class StatefulPersistenceContext method addCollection.

/**
	 * Add an collection to the cache, with a given collection entry.
	 *
	 * @param coll The collection for which we are adding an entry.
	 * @param entry The entry representing the collection.
	 * @param key The key of the collection's entry.
	 */
private void addCollection(PersistentCollection coll, CollectionEntry entry, Serializable key) {
    collectionEntries.put(coll, entry);
    final CollectionKey collectionKey = new CollectionKey(entry.getLoadedPersister(), key);
    final PersistentCollection old = collectionsByKey.put(collectionKey, coll);
    if (old != null) {
        if (old == coll) {
            throw new AssertionFailure("bug adding collection twice");
        }
        // or should it actually throw an exception?
        old.unsetSession(session);
        collectionEntries.remove(old);
    // watch out for a case where old is still referenced
    // somewhere in the object graph! (which is a user error)
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) AssertionFailure(org.hibernate.AssertionFailure) CollectionKey(org.hibernate.engine.spi.CollectionKey)

Example 33 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class StatefulPersistenceContext method isReadOnly.

@Override
public boolean isReadOnly(Object entityOrProxy) {
    if (entityOrProxy == null) {
        throw new AssertionFailure("object must be non-null.");
    }
    boolean isReadOnly;
    if (entityOrProxy instanceof HibernateProxy) {
        isReadOnly = ((HibernateProxy) entityOrProxy).getHibernateLazyInitializer().isReadOnly();
    } else {
        final EntityEntry ee = getEntry(entityOrProxy);
        if (ee == null) {
            throw new TransientObjectException("Instance was not associated with this persistence context");
        }
        isReadOnly = ee.isReadOnly();
    }
    return isReadOnly;
}
Also used : TransientObjectException(org.hibernate.TransientObjectException) EntityEntry(org.hibernate.engine.spi.EntityEntry) AssertionFailure(org.hibernate.AssertionFailure) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 34 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class StatefulPersistenceContext method setReadOnly.

@Override
public void setReadOnly(Object object, boolean readOnly) {
    if (object == null) {
        throw new AssertionFailure("object must be non-null.");
    }
    if (isReadOnly(object) == readOnly) {
        return;
    }
    if (object instanceof HibernateProxy) {
        final HibernateProxy proxy = (HibernateProxy) object;
        setProxyReadOnly(proxy, readOnly);
        if (Hibernate.isInitialized(proxy)) {
            setEntityReadOnly(proxy.getHibernateLazyInitializer().getImplementation(), readOnly);
        }
    } else {
        setEntityReadOnly(object, readOnly);
        // PersistenceContext.proxyFor( entity ) returns entity if there is no proxy for that entity
        // so need to check the return value to be sure it is really a proxy
        final Object maybeProxy = getSession().getPersistenceContext().proxyFor(object);
        if (maybeProxy instanceof HibernateProxy) {
            setProxyReadOnly((HibernateProxy) maybeProxy, readOnly);
        }
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 35 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class Collections method processDereferencedCollection.

private static void processDereferencedCollection(PersistentCollection coll, SessionImplementor session) {
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final CollectionEntry entry = persistenceContext.getCollectionEntry(coll);
    final CollectionPersister loadedPersister = entry.getLoadedPersister();
    if (loadedPersister != null && LOG.isDebugEnabled()) {
        LOG.debugf("Collection dereferenced: %s", MessageHelper.collectionInfoString(loadedPersister, coll, entry.getLoadedKey(), session));
    }
    // do a check
    final boolean hasOrphanDelete = loadedPersister != null && loadedPersister.hasOrphanDelete();
    if (hasOrphanDelete) {
        Serializable ownerId = loadedPersister.getOwnerEntityPersister().getIdentifier(coll.getOwner(), session);
        if (ownerId == null) {
            // the persistence context
            if (session.getFactory().getSessionFactoryOptions().isIdentifierRollbackEnabled()) {
                final EntityEntry ownerEntry = persistenceContext.getEntry(coll.getOwner());
                if (ownerEntry != null) {
                    ownerId = ownerEntry.getId();
                }
            }
            if (ownerId == null) {
                throw new AssertionFailure("Unable to determine collection owner identifier for orphan-delete processing");
            }
        }
        final EntityKey key = session.generateEntityKey(ownerId, loadedPersister.getOwnerEntityPersister());
        final Object owner = persistenceContext.getEntity(key);
        if (owner == null) {
            throw new AssertionFailure("collection owner not associated with session: " + loadedPersister.getRole());
        }
        final EntityEntry e = persistenceContext.getEntry(owner);
        //only collections belonging to deleted entities are allowed to be dereferenced in the case of orphan delete
        if (e != null && e.getStatus() != Status.DELETED && e.getStatus() != Status.GONE) {
            throw new HibernateException("A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: " + loadedPersister.getRole());
        }
    }
    // do the work
    entry.setCurrentPersister(null);
    entry.setCurrentKey(null);
    prepareCollectionForUpdate(coll, entry, session.getFactory());
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) Serializable(java.io.Serializable) EntityEntry(org.hibernate.engine.spi.EntityEntry) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) AssertionFailure(org.hibernate.AssertionFailure) HibernateException(org.hibernate.HibernateException) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Aggregations

AssertionFailure (org.hibernate.AssertionFailure)54 EntityEntry (org.hibernate.engine.spi.EntityEntry)11 Serializable (java.io.Serializable)10 Property (org.hibernate.mapping.Property)10 AnnotationException (org.hibernate.AnnotationException)8 PersistentClass (org.hibernate.mapping.PersistentClass)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 SQLException (java.sql.SQLException)6 HibernateException (org.hibernate.HibernateException)6 SimpleValue (org.hibernate.mapping.SimpleValue)6 PreparedStatement (java.sql.PreparedStatement)5 HashMap (java.util.HashMap)5 Component (org.hibernate.mapping.Component)5 Join (org.hibernate.mapping.Join)5 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)5 Iterator (java.util.Iterator)4 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)4 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)4 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)4 ManyToOne (org.hibernate.mapping.ManyToOne)4