Search in sources :

Example 96 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class EntityKey method deserialize.

/**
	 * Custom deserialization routine used during deserialization of a
	 * Session/PersistenceContext for increased performance.
	 *
	 * @param ois The stream from which to read the entry.
	 * @param sessionFactory The SessionFactory owning the Session being deserialized.
	 *
	 * @return The deserialized EntityEntry
	 *
	 * @throws IOException Thrown by Java I/O
	 * @throws ClassNotFoundException Thrown by Java I/O
	 */
public static EntityKey deserialize(ObjectInputStream ois, SessionFactoryImplementor sessionFactory) throws IOException, ClassNotFoundException {
    final Serializable id = (Serializable) ois.readObject();
    final String entityName = (String) ois.readObject();
    final EntityPersister entityPersister = sessionFactory.getEntityPersister(entityName);
    return new EntityKey(id, entityPersister);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable)

Example 97 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class DefaultLoadEventListener method loadFromSessionCache.

/**
	 * Attempts to locate the entity in the session-level cache.
	 * <p/>
	 * If allowed to return nulls, then if the entity happens to be found in
	 * the session cache, we check the entity type for proper handling
	 * of entity hierarchies.
	 * <p/>
	 * If checkDeleted was set to true, then if the entity is found in the
	 * session-level cache, it's current status within the session cache
	 * is checked to see if it has previously been scheduled for deletion.
	 *
	 * @param event The load event
	 * @param keyToLoad The EntityKey representing the entity to be loaded.
	 * @param options The load options.
	 *
	 * @return The entity from the session-level cache, or null.
	 *
	 * @throws HibernateException Generally indicates problems applying a lock-mode.
	 */
protected Object loadFromSessionCache(final LoadEvent event, final EntityKey keyToLoad, final LoadEventListener.LoadType options) throws HibernateException {
    SessionImplementor session = event.getSession();
    Object old = session.getEntityUsingInterceptor(keyToLoad);
    if (old != null) {
        // this object was already loaded
        EntityEntry oldEntry = session.getPersistenceContext().getEntry(old);
        if (options.isCheckDeleted()) {
            Status status = oldEntry.getStatus();
            if (status == Status.DELETED || status == Status.GONE) {
                return REMOVED_ENTITY_MARKER;
            }
        }
        if (options.isAllowNulls()) {
            final EntityPersister persister = event.getSession().getFactory().getEntityPersister(keyToLoad.getEntityName());
            if (!persister.isInstance(old)) {
                return INCONSISTENT_RTN_CLASS_MARKER;
            }
        }
        upgradeLock(old, oldEntry, event.getLockOptions(), event.getSession());
    }
    return old;
}
Also used : Status(org.hibernate.engine.spi.Status) EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityEntry(org.hibernate.engine.spi.EntityEntry) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 98 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class DefaultLoadEventListener method onLoad.

/**
	 * Handle the given load event.
	 *
	 * @param event The load event to be handled.
	 *
	 * @throws HibernateException
	 */
public void onLoad(final LoadEvent event, final LoadEventListener.LoadType loadType) throws HibernateException {
    final EntityPersister persister = getPersister(event);
    if (persister == null) {
        throw new HibernateException("Unable to locate persister: " + event.getEntityClassName());
    }
    final Class idClass = persister.getIdentifierType().getReturnedClass();
    if (idClass != null && !idClass.isInstance(event.getEntityId())) {
        checkIdClass(persister, event, loadType, idClass);
    }
    doOnLoad(persister, event, loadType);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) HibernateException(org.hibernate.HibernateException)

Example 99 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class DefaultMergeEventListener method entityIsPersistent.

protected void entityIsPersistent(MergeEvent event, Map copyCache) {
    LOG.trace("Ignoring persistent instance");
    //TODO: check that entry.getIdentifier().equals(requestedId)
    final Object entity = event.getEntity();
    final EventSource source = event.getSession();
    final EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
    //beforeQuery cascade!
    ((MergeContext) copyCache).put(entity, entity, true);
    cascadeOnMerge(source, persister, entity, copyCache);
    copyValues(persister, entity, entity, source, copyCache);
    event.setResult(entity);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EventSource(org.hibernate.event.spi.EventSource)

Example 100 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class DefaultSaveOrUpdateEventListener method entityIsDetached.

/**
	 * The given save-update event named a detached entity.
	 * <p/>
	 * Here, we will perform the update processing.
	 *
	 * @param event The update event to be handled.
	 */
protected void entityIsDetached(SaveOrUpdateEvent event) {
    LOG.trace("Updating detached instance");
    if (event.getSession().getPersistenceContext().isEntryFor(event.getEntity())) {
        //TODO: assertion only, could be optimized away
        throw new AssertionFailure("entity was persistent");
    }
    Object entity = event.getEntity();
    EntityPersister persister = event.getSession().getEntityPersister(event.getEntityName(), entity);
    event.setRequestedId(getUpdateId(entity, persister, event.getRequestedId(), event.getSession()));
    performUpdate(event, entity, persister);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AssertionFailure(org.hibernate.AssertionFailure)

Aggregations

EntityPersister (org.hibernate.persister.entity.EntityPersister)166 Test (org.junit.Test)59 Serializable (java.io.Serializable)34 Session (org.hibernate.Session)31 Type (org.hibernate.type.Type)29 EntityEntry (org.hibernate.engine.spi.EntityEntry)21 EventSource (org.hibernate.event.spi.EventSource)15 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)13 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)13 TestForIssue (org.hibernate.testing.TestForIssue)13 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)12 SequenceStyleGenerator (org.hibernate.id.enhanced.SequenceStyleGenerator)12 EntityType (org.hibernate.type.EntityType)12 ArrayList (java.util.ArrayList)11 CompositeType (org.hibernate.type.CompositeType)11 HibernateProxy (org.hibernate.proxy.HibernateProxy)10 PreparedStatement (java.sql.PreparedStatement)8 List (java.util.List)8 EntityKey (org.hibernate.engine.spi.EntityKey)8 QueryParameters (org.hibernate.engine.spi.QueryParameters)8