Search in sources :

Example 91 with SessionImplementor

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

the class ASTParserLoadingTest method testInvalidFetchSemantics.

@Test
public void testInvalidFetchSemantics() {
    try (final SessionImplementor s = (SessionImplementor) openSession()) {
        inTransaction(s, session -> {
            try {
                s.createQuery("select mother from Human a left join fetch a.mother mother").list();
                fail("invalid fetch semantic allowed!");
            } catch (IllegalArgumentException e) {
                assertTyping(QueryException.class, e.getCause());
            } catch (QueryException e) {
            }
        });
        inTransaction(s, session -> {
            try {
                s.createQuery("select mother from Human a left join fetch a.mother mother").list();
                fail("invalid fetch semantic allowed!");
            } catch (IllegalArgumentException e) {
                assertTyping(QueryException.class, e.getCause());
            } catch (QueryException e) {
            }
        });
    }
}
Also used : QueryException(org.hibernate.QueryException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Test(org.junit.Test)

Example 92 with SessionImplementor

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

the class ASTParserLoadingTest method testMultipleBagFetchesFail.

@Test
public void testMultipleBagFetchesFail() {
    try (final SessionImplementor s = (SessionImplementor) openSession()) {
        inTransaction(s, session -> {
            try {
                s.createQuery("from Human h join fetch h.friends f join fetch f.friends fof").list();
                fail("failure expected");
            } catch (IllegalArgumentException e) {
                assertTyping(MultipleBagFetchException.class, e.getCause());
            } catch (HibernateException e) {
                assertTrue("unexpected failure reason : " + e, e.getMessage().indexOf("multiple bags") > 0);
            }
        });
    }
}
Also used : HibernateException(org.hibernate.HibernateException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException) Test(org.junit.Test)

Example 93 with SessionImplementor

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

the class CaseStatementTest method testSimpleCaseStatementWithParamAllResults.

@Test
public void testSimpleCaseStatementWithParamAllResults() {
    try (final SessionImplementor s = (SessionImplementor) openSession()) {
        inTransaction(s, session -> {
            try {
                s.createQuery("select case p.name when 'Steve' then :opt1 else :opt2 end from Person p").setString("opt1", "x").setString("opt2", "y").list();
                fail("was expecting an exception");
            } catch (IllegalArgumentException e) {
                assertTyping(QueryException.class, e.getCause());
            } catch (QueryException expected) {
            // expected
            }
        });
        inTransaction(s, session -> {
            s.createQuery("select case p.name when 'Steve' then cast( :opt1 as string ) else cast( :opt2 as string) end from Person p").setString("opt1", "x").setString("opt2", "y").list();
        });
        inTransaction(s, session -> {
            try {
                s.createQuery("select case p.name when 'Steve' then :opt1 else :opt2 end from Person p").setString("opt1", "x").setString("opt2", "y").list();
                fail("was expecting an exception");
            } catch (IllegalArgumentException e) {
                assertTyping(QueryException.class, e.getCause());
            } catch (QueryException expected) {
            // expected
            }
        });
    }
}
Also used : QueryException(org.hibernate.QueryException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Test(org.junit.Test)

Example 94 with SessionImplementor

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

the class CacheLazyLoadNoTransTest method isCached.

private boolean isCached(Serializable id, Class<?> entityClass, String attr) {
    Session session = openSession();
    CollectionPersister persister = sessionFactory().getCollectionPersister(entityClass.getName() + "." + attr);
    CollectionDataAccess cache = persister.getCacheAccessStrategy();
    Object key = cache.generateCacheKey(id, persister, sessionFactory(), session.getTenantIdentifier());
    Object cachedValue = cache.get(((SessionImplementor) session), key);
    session.close();
    return cachedValue != null;
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess) Session(org.hibernate.Session)

Example 95 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project spring-framework by spring-projects.

the class HibernateTransactionManager method doCleanupAfterCompletion.

@Override
protected void doCleanupAfterCompletion(Object transaction) {
    HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
    // Remove the session holder from the thread.
    if (txObject.isNewSessionHolder()) {
        TransactionSynchronizationManager.unbindResource(obtainSessionFactory());
    }
    // Remove the JDBC connection holder from the thread, if exposed.
    if (getDataSource() != null) {
        TransactionSynchronizationManager.unbindResource(getDataSource());
    }
    SessionImplementor session = txObject.getSessionHolder().getSession().unwrap(SessionImplementor.class);
    if (txObject.needsConnectionReset() && session.getJdbcCoordinator().getLogicalConnection().isPhysicallyConnected()) {
        // Else, we need to rely on the connection pool to perform proper cleanup.
        try {
            Connection con = session.connection();
            Integer previousHoldability = txObject.getPreviousHoldability();
            if (previousHoldability != null) {
                con.setHoldability(previousHoldability);
            }
            DataSourceUtils.resetConnectionAfterTransaction(con, txObject.getPreviousIsolationLevel(), txObject.isReadOnly());
        } catch (HibernateException ex) {
            logger.debug("Could not access JDBC Connection of Hibernate Session", ex);
        } catch (Throwable ex) {
            logger.debug("Could not reset JDBC Connection after transaction", ex);
        }
    }
    if (txObject.isNewSession()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Closing Hibernate Session [" + session + "] after transaction");
        }
        SessionFactoryUtils.closeSession(session);
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Not closing pre-bound Hibernate Session [" + session + "] after transaction");
        }
        if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
            session.setHibernateFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
        }
        if (!this.allowResultAccessAfterCompletion && !this.hibernateManagedSession) {
            disconnectOnCompletion(session);
        }
    }
    txObject.getSessionHolder().clear();
}
Also used : HibernateException(org.hibernate.HibernateException) Connection(java.sql.Connection) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Aggregations

SessionImplementor (org.hibernate.engine.spi.SessionImplementor)97 Test (org.junit.Test)60 Session (org.hibernate.Session)58 Connection (java.sql.Connection)20 TestForIssue (org.hibernate.testing.TestForIssue)18 PreparedStatement (java.sql.PreparedStatement)17 EntityPersister (org.hibernate.persister.entity.EntityPersister)14 Work (org.hibernate.jdbc.Work)13 Statement (java.sql.Statement)12 List (java.util.List)12 Transaction (org.hibernate.Transaction)12 ResultSet (java.sql.ResultSet)11 SQLException (java.sql.SQLException)11 Serializable (java.io.Serializable)8 ArrayList (java.util.ArrayList)7 EntityEntry (org.hibernate.engine.spi.EntityEntry)7 JDBCException (org.hibernate.JDBCException)6 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)6 QueryParameters (org.hibernate.engine.spi.QueryParameters)6 ResultSetProcessor (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor)6