Search in sources :

Example 61 with SessionImplementor

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

the class DefaultFlushEntityEventListener method handleInterception.

protected boolean handleInterception(FlushEntityEvent event) {
    SessionImplementor session = event.getSession();
    EntityEntry entry = event.getEntityEntry();
    EntityPersister persister = entry.getPersister();
    Object entity = event.getEntity();
    //give the Interceptor a chance to modify property values
    final Object[] values = event.getPropertyValues();
    final boolean intercepted = invokeInterceptor(session, entity, entry, values, persister);
    //now we might need to recalculate the dirtyProperties array
    if (intercepted && event.isDirtyCheckPossible() && !event.isDirtyCheckHandledByInterceptor()) {
        int[] dirtyProperties;
        if (event.hasDatabaseSnapshot()) {
            dirtyProperties = persister.findModified(event.getDatabaseSnapshot(), values, entity, session);
        } else {
            dirtyProperties = persister.findDirty(values, entry.getLoadedState(), entity, session);
        }
        event.setDirtyProperties(dirtyProperties);
    }
    return intercepted;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityEntry(org.hibernate.engine.spi.EntityEntry) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 62 with SessionImplementor

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

the class DefaultLoadEventListener method loadFromSecondLevelCache.

/**
	 * Attempts to load the entity from the second-level cache.
	 *
	 * @param event The load event
	 * @param persister The persister for the entity being requested for load
	 *
	 * @return The entity from the second-level cache, or null.
	 */
private Object loadFromSecondLevelCache(final LoadEvent event, final EntityPersister persister, final EntityKey entityKey) {
    final SessionImplementor source = event.getSession();
    final boolean useCache = persister.hasCache() && source.getCacheMode().isGetEnabled() && event.getLockMode().lessThan(LockMode.READ);
    if (!useCache) {
        // we can't use cache here
        return null;
    }
    final Object ce = getFromSharedCache(event, persister, source);
    if (ce == null) {
        // nothing was found in cache
        return null;
    }
    return processCachedEntry(event, persister, ce, source, entityKey);
}
Also used : SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 63 with SessionImplementor

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

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

the class DirtyCollectionSearchVisitor method processCollection.

Object processCollection(Object collection, CollectionType type) throws HibernateException {
    if (collection != null) {
        final SessionImplementor session = getSession();
        final PersistentCollection persistentCollection;
        if (type.isArrayType()) {
            persistentCollection = session.getPersistenceContext().getCollectionHolder(collection);
        // if no array holder we found an unwrappered array (this can't occur,
        // because we now always call wrap() beforeQuery getting to here)
        // return (ah==null) ? true : searchForDirtyCollections(ah, type);
        } else {
            // if not wrappered yet, its dirty (this can't occur, because
            // we now always call wrap() beforeQuery getting to here)
            // return ( ! (obj instanceof PersistentCollection) ) ?
            //true : searchForDirtyCollections( (PersistentCollection) obj, type );
            persistentCollection = (PersistentCollection) collection;
        }
        if (persistentCollection.isDirty()) {
            //we need to check even if it was not initialized, because of delayed adds!
            dirty = true;
            //NOTE: EARLY EXIT!
            return null;
        }
    }
    return null;
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 65 with SessionImplementor

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

the class BasicOperationsTest method testCreateAndDelete.

@Test
public void testCreateAndDelete() {
    Date now = new Date();
    Session s = openSession();
    s.doWork(new ValidateSomeEntityColumns((SessionImplementor) s));
    s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_ENTITY_TABLE_NAME, 0));
    s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_OTHER_ENTITY_TABLE_NAME, 0));
    s.beginTransaction();
    SomeEntity someEntity = new SomeEntity(now);
    SomeOtherEntity someOtherEntity = new SomeOtherEntity(1);
    s.save(someEntity);
    s.save(someOtherEntity);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_ENTITY_TABLE_NAME, 1));
    s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_OTHER_ENTITY_TABLE_NAME, 1));
    s.beginTransaction();
    s.delete(someEntity);
    s.delete(someOtherEntity);
    s.getTransaction().commit();
    s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_ENTITY_TABLE_NAME, 0));
    s.doWork(new ValidateRowCount((SessionImplementor) s, SOME_OTHER_ENTITY_TABLE_NAME, 0));
    s.close();
}
Also used : SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

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