Search in sources :

Example 11 with SessionImplementor

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

the class DefaultInitializeCollectionEventListener method onInitializeCollection.

/**
	 * called by a collection that wants to initialize itself
	 */
public void onInitializeCollection(InitializeCollectionEvent event) throws HibernateException {
    PersistentCollection collection = event.getCollection();
    SessionImplementor source = event.getSession();
    CollectionEntry ce = source.getPersistenceContext().getCollectionEntry(collection);
    if (ce == null) {
        throw new HibernateException("collection was evicted");
    }
    if (!collection.wasInitialized()) {
        final boolean traceEnabled = LOG.isTraceEnabled();
        if (traceEnabled) {
            LOG.tracev("Initializing collection {0}", MessageHelper.collectionInfoString(ce.getLoadedPersister(), collection, ce.getLoadedKey(), source));
            LOG.trace("Checking second-level cache");
        }
        final boolean foundInCache = initializeCollectionFromCache(ce.getLoadedKey(), ce.getLoadedPersister(), collection, source);
        if (foundInCache) {
            if (traceEnabled) {
                LOG.trace("Collection initialized from cache");
            }
        } else {
            if (traceEnabled) {
                LOG.trace("Collection not cached");
            }
            ce.getLoadedPersister().initialize(ce.getLoadedKey(), source);
            if (traceEnabled) {
                LOG.trace("Collection initialized");
            }
            if (source.getFactory().getStatistics().isStatisticsEnabled()) {
                source.getFactory().getStatisticsImplementor().fetchCollection(ce.getLoadedPersister().getRole());
            }
        }
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) HibernateException(org.hibernate.HibernateException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 12 with SessionImplementor

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

the class DefaultLockEventListener method onLock.

/**
	 * Handle the given lock event.
	 *
	 * @param event The lock event to be handled.
	 * @throws HibernateException
	 */
public void onLock(LockEvent event) throws HibernateException {
    if (event.getObject() == null) {
        throw new NullPointerException("attempted to lock null");
    }
    if (event.getLockMode() == LockMode.WRITE) {
        throw new HibernateException("Invalid lock mode for lock()");
    }
    if (event.getLockMode() == LockMode.UPGRADE_SKIPLOCKED) {
        LOG.explicitSkipLockedLockCombo();
    }
    SessionImplementor source = event.getSession();
    Object entity = source.getPersistenceContext().unproxyAndReassociate(event.getObject());
    //TODO: if object was an uninitialized proxy, this is inefficient,
    //      resulting in two SQL selects
    EntityEntry entry = source.getPersistenceContext().getEntry(entity);
    if (entry == null) {
        final EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
        final Serializable id = persister.getIdentifier(entity, source);
        if (!ForeignKeys.isNotTransient(event.getEntityName(), entity, Boolean.FALSE, source)) {
            throw new TransientObjectException("cannot lock an unsaved transient instance: " + persister.getEntityName());
        }
        entry = reassociate(event, entity, id, persister);
        cascadeOnLock(event, persister, entity);
    }
    upgradeLock(entity, entry, event.getLockOptions(), event.getSession());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) TransientObjectException(org.hibernate.TransientObjectException) EntityEntry(org.hibernate.engine.spi.EntityEntry) Serializable(java.io.Serializable) HibernateException(org.hibernate.HibernateException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 13 with SessionImplementor

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

the class WrapVisitor method processCollection.

@Override
Object processCollection(Object collection, CollectionType collectionType) throws HibernateException {
    if (collection != null && (collection instanceof PersistentCollection)) {
        final SessionImplementor session = getSession();
        PersistentCollection coll = (PersistentCollection) collection;
        if (coll.setCurrentSession(session)) {
            reattachCollection(coll, collectionType);
        }
        return null;
    } else {
        return processArrayOrNewCollection(collection, collectionType);
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 14 with SessionImplementor

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

the class WrapVisitor method processArrayOrNewCollection.

final Object processArrayOrNewCollection(Object collection, CollectionType collectionType) throws HibernateException {
    final SessionImplementor session = getSession();
    if (collection == null) {
        //do nothing
        return null;
    } else {
        CollectionPersister persister = session.getFactory().getCollectionPersister(collectionType.getRole());
        final PersistenceContext persistenceContext = session.getPersistenceContext();
        //TODO: move into collection type, so we can use polymorphism!
        if (collectionType.hasHolder()) {
            if (collection == CollectionType.UNFETCHED_COLLECTION) {
                return null;
            }
            PersistentCollection ah = persistenceContext.getCollectionHolder(collection);
            if (ah == null) {
                ah = collectionType.wrap(session, collection);
                persistenceContext.addNewCollection(persister, ah);
                persistenceContext.addCollectionHolder(ah);
            }
            return null;
        } else {
            PersistentCollection persistentCollection = collectionType.wrap(session, collection);
            persistenceContext.addNewCollection(persister, persistentCollection);
            if (LOG.isTraceEnabled()) {
                LOG.tracev("Wrapped collection in role: {0}", collectionType.getRole());
            }
            //Force a substitution!
            return persistentCollection;
        }
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 15 with SessionImplementor

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

the class DefaultSaveOrUpdateEventListener method onSaveOrUpdate.

/**
	 * Handle the given update event.
	 *
	 * @param event The update event to be handled.
	 */
public void onSaveOrUpdate(SaveOrUpdateEvent event) {
    final SessionImplementor source = event.getSession();
    final Object object = event.getObject();
    final Serializable requestedId = event.getRequestedId();
    if (requestedId != null) {
        //reassociating the proxy
        if (object instanceof HibernateProxy) {
            ((HibernateProxy) object).getHibernateLazyInitializer().setIdentifier(requestedId);
        }
    }
    // For an uninitialized proxy, noop, don't even need to return an id, since it is never a save()
    if (reassociateIfUninitializedProxy(object, source)) {
        LOG.trace("Reassociated uninitialized proxy");
    } else {
        //initialize properties of the event:
        final Object entity = source.getPersistenceContext().unproxyAndReassociate(object);
        event.setEntity(entity);
        event.setEntry(source.getPersistenceContext().getEntry(entity));
        //return the id in the event object
        event.setResultId(performSaveOrUpdate(event));
    }
}
Also used : Serializable(java.io.Serializable) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Aggregations

SessionImplementor (org.hibernate.engine.spi.SessionImplementor)82 Session (org.hibernate.Session)59 Test (org.junit.Test)54 Connection (java.sql.Connection)20 TestForIssue (org.hibernate.testing.TestForIssue)18 PreparedStatement (java.sql.PreparedStatement)17 Work (org.hibernate.jdbc.Work)13 Statement (java.sql.Statement)12 List (java.util.List)12 Transaction (org.hibernate.Transaction)12 EntityPersister (org.hibernate.persister.entity.EntityPersister)12 ResultSet (java.sql.ResultSet)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)7 JDBCException (org.hibernate.JDBCException)6 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)6 EntityEntry (org.hibernate.engine.spi.EntityEntry)6 QueryParameters (org.hibernate.engine.spi.QueryParameters)6 ResultSetProcessor (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor)6 NamedParameterContext (org.hibernate.loader.plan.exec.query.spi.NamedParameterContext)6