Search in sources :

Example 1 with EntityKey

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

the class StatefulPersistenceContext method getDatabaseSnapshot.

/**
	 * Get the current state of the entity as known to the underlying
	 * database, or null if there is no corresponding row
	 * <p/>
	 * {@inheritDoc}
	 */
@Override
public Object[] getDatabaseSnapshot(Serializable id, EntityPersister persister) throws HibernateException {
    final EntityKey key = session.generateEntityKey(id, persister);
    final Object cached = entitySnapshotsByKey.get(key);
    if (cached != null) {
        return cached == NO_ROW ? null : (Object[]) cached;
    } else {
        final Object[] snapshot = persister.getDatabaseSnapshot(id, session);
        entitySnapshotsByKey.put(key, snapshot == null ? NO_ROW : snapshot);
        return snapshot;
    }
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey)

Example 2 with EntityKey

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

the class StatefulPersistenceContext method replaceDelayedEntityIdentityInsertKeys.

@Override
public void replaceDelayedEntityIdentityInsertKeys(EntityKey oldKey, Serializable generatedId) {
    final Object entity = entitiesByKey.remove(oldKey);
    final EntityEntry oldEntry = entityEntryContext.removeEntityEntry(entity);
    parentsByChild.clear();
    final EntityKey newKey = session.generateEntityKey(generatedId, oldEntry.getPersister());
    addEntity(newKey, entity);
    addEntry(entity, oldEntry.getStatus(), oldEntry.getLoadedState(), oldEntry.getRowId(), generatedId, oldEntry.getVersion(), oldEntry.getLockMode(), oldEntry.isExistsInDatabase(), oldEntry.getPersister(), oldEntry.isBeingReplicated());
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) EntityEntry(org.hibernate.engine.spi.EntityEntry)

Example 3 with EntityKey

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

the class AbstractReassociateEventListener method reassociate.

/**
	 * Associates a given entity (either transient or associated with another session) to
	 * the given session.
	 *
	 * @param event The event triggering the re-association
	 * @param object The entity to be associated
	 * @param id The id of the entity.
	 * @param persister The entity's persister instance.
	 *
	 * @return An EntityEntry representing the entity within this session.
	 */
protected final EntityEntry reassociate(AbstractEvent event, Object object, Serializable id, EntityPersister persister) {
    if (log.isTraceEnabled()) {
        log.tracev("Reassociating transient instance: {0}", MessageHelper.infoString(persister, id, event.getSession().getFactory()));
    }
    final EventSource source = event.getSession();
    final EntityKey key = source.generateEntityKey(id, persister);
    source.getPersistenceContext().checkUniqueness(key, object);
    //get a snapshot
    Object[] values = persister.getPropertyValues(object);
    TypeHelper.deepCopy(values, persister.getPropertyTypes(), persister.getPropertyUpdateability(), values, source);
    Object version = Versioning.getVersion(values, persister);
    EntityEntry newEntry = source.getPersistenceContext().addEntity(object, (persister.isMutable() ? Status.MANAGED : Status.READ_ONLY), values, key, version, LockMode.NONE, true, persister, false);
    new OnLockVisitor(source, id, object).process(object, persister);
    persister.afterReassociate(object, source);
    return newEntry;
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry)

Example 4 with EntityKey

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

the class DefaultDeleteEventListener method onDelete.

/**
	 * Handle the given delete event.  This is the cascaded form.
	 *
	 * @param event The delete event.
	 * @param transientEntities The cache of entities already deleted
	 *
	 * @throws HibernateException
	 */
public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {
    final EventSource source = event.getSession();
    final PersistenceContext persistenceContext = source.getPersistenceContext();
    Object entity = persistenceContext.unproxyAndReassociate(event.getObject());
    EntityEntry entityEntry = persistenceContext.getEntry(entity);
    final EntityPersister persister;
    final Serializable id;
    final Object version;
    if (entityEntry == null) {
        LOG.trace("Entity was not persistent in delete processing");
        persister = source.getEntityPersister(event.getEntityName(), entity);
        if (ForeignKeys.isTransient(persister.getEntityName(), entity, null, source)) {
            deleteTransientEntity(source, entity, event.isCascadeDeleteEnabled(), persister, transientEntities);
            // EARLY EXIT!!!
            return;
        }
        performDetachedEntityDeletionCheck(event);
        id = persister.getIdentifier(entity, source);
        if (id == null) {
            throw new TransientObjectException("the detached instance passed to delete() had a null identifier");
        }
        final EntityKey key = source.generateEntityKey(id, persister);
        persistenceContext.checkUniqueness(key, entity);
        new OnUpdateVisitor(source, id, entity).process(entity, persister);
        version = persister.getVersion(entity);
        entityEntry = persistenceContext.addEntity(entity, (persister.isMutable() ? Status.MANAGED : Status.READ_ONLY), persister.getPropertyValues(entity), key, version, LockMode.NONE, true, persister, false);
    } else {
        LOG.trace("Deleting a persistent instance");
        if (entityEntry.getStatus() == Status.DELETED || entityEntry.getStatus() == Status.GONE) {
            LOG.trace("Object was already deleted");
            return;
        }
        persister = entityEntry.getPersister();
        id = entityEntry.getId();
        version = entityEntry.getVersion();
    }
    if (invokeDeleteLifecycle(source, entity, persister)) {
        return;
    }
    deleteEntity(source, entity, entityEntry, event.isCascadeDeleteEnabled(), event.isOrphanRemovalBeforeUpdates(), persister, transientEntities);
    if (source.getFactory().getSettings().isIdentifierRollbackEnabled()) {
        persister.resetIdentifier(entity, id, version, source);
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) TransientObjectException(org.hibernate.TransientObjectException) EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry) Serializable(java.io.Serializable) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 5 with EntityKey

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

the class DefaultEvictEventListener method onEvict.

/**
	 * Handle the given evict event.
	 *
	 * @param event The evict event to be handled.
	 *
	 * @throws HibernateException
	 */
public void onEvict(EvictEvent event) throws HibernateException {
    final Object object = event.getObject();
    if (object == null) {
        throw new NullPointerException("null passed to Session.evict()");
    }
    final EventSource source = event.getSession();
    final PersistenceContext persistenceContext = source.getPersistenceContext();
    if (object instanceof HibernateProxy) {
        final LazyInitializer li = ((HibernateProxy) object).getHibernateLazyInitializer();
        final Serializable id = li.getIdentifier();
        if (id == null) {
            throw new IllegalArgumentException("Could not determine identifier of proxy passed to evict()");
        }
        final EntityPersister persister = source.getFactory().getEntityPersister(li.getEntityName());
        final EntityKey key = source.generateEntityKey(id, persister);
        persistenceContext.removeProxy(key);
        if (!li.isUninitialized()) {
            final Object entity = persistenceContext.removeEntity(key);
            if (entity != null) {
                EntityEntry e = persistenceContext.removeEntry(entity);
                doEvict(entity, key, e.getPersister(), event.getSession());
            }
        }
        li.unsetSession();
    } else {
        EntityEntry e = persistenceContext.removeEntry(object);
        if (e != null) {
            persistenceContext.removeEntity(e.getEntityKey());
            doEvict(object, e.getEntityKey(), e.getPersister(), source);
        } else {
            // see if the passed object is even an entity, and if not throw an exception
            // 		this is different than legacy Hibernate behavior, but what JPA 2.1 is calling for
            //		with EntityManager.detach
            EntityPersister persister = null;
            final String entityName = persistenceContext.getSession().guessEntityName(object);
            if (entityName != null) {
                try {
                    persister = persistenceContext.getSession().getFactory().getEntityPersister(entityName);
                } catch (Exception ignore) {
                }
            }
            if (persister == null) {
                throw new IllegalArgumentException("Non-entity object instance passed to evict : " + object);
            }
        }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) LazyInitializer(org.hibernate.proxy.LazyInitializer) Serializable(java.io.Serializable) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) HibernateProxy(org.hibernate.proxy.HibernateProxy) HibernateException(org.hibernate.HibernateException) EntityKey(org.hibernate.engine.spi.EntityKey) EventSource(org.hibernate.event.spi.EventSource) EntityEntry(org.hibernate.engine.spi.EntityEntry)

Aggregations

EntityKey (org.hibernate.engine.spi.EntityKey)43 Serializable (java.io.Serializable)13 EntityEntry (org.hibernate.engine.spi.EntityEntry)12 EntityPersister (org.hibernate.persister.entity.EntityPersister)10 EventSource (org.hibernate.event.spi.EventSource)8 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)7 List (java.util.List)6 EntityType (org.hibernate.type.EntityType)6 ArrayList (java.util.ArrayList)5 SQLException (java.sql.SQLException)4 EntityReferenceProcessingState (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessingContext.EntityReferenceProcessingState)4 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 HibernateException (org.hibernate.HibernateException)3 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)3 SubselectFetch (org.hibernate.engine.spi.SubselectFetch)3 HibernateProxy (org.hibernate.proxy.HibernateProxy)3 Type (org.hibernate.type.Type)3 HashSet (java.util.HashSet)2