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) {
}
});
}
}
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);
}
});
}
}
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
}
});
}
}
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;
}
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();
}
Aggregations