use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class TransactionTimeoutTest method testJdbcCoordinatorTransactionTimeoutCheck.
@Test
public void testJdbcCoordinatorTransactionTimeoutCheck() {
Session session = openSession();
Transaction transaction = session.getTransaction();
transaction.setTimeout(2);
assertEquals(-1, ((SessionImplementor) session).getJdbcCoordinator().determineRemainingTransactionTimeOutPeriod());
transaction.begin();
assertNotSame(-1, ((SessionImplementor) session).getJdbcCoordinator().determineRemainingTransactionTimeOutPeriod());
transaction.commit();
session.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class NaturalIdCacheKeyTest method testSerializationRoundTrip.
@Test
public void testSerializationRoundTrip() throws Exception {
final EntityPersister entityPersister = mock(EntityPersister.class);
final SessionImplementor sessionImplementor = mock(SessionImplementor.class);
final SessionFactoryImplementor sessionFactoryImplementor = mock(SessionFactoryImplementor.class);
final Type mockType = mock(Type.class);
when(entityPersister.getRootEntityName()).thenReturn("EntityName");
when(sessionImplementor.getFactory()).thenReturn(sessionFactoryImplementor);
when(entityPersister.getNaturalIdentifierProperties()).thenReturn(new int[] { 0, 1, 2 });
when(entityPersister.getPropertyTypes()).thenReturn(new Type[] { mockType, mockType, mockType });
when(mockType.getHashCode(anyObject(), eq(sessionFactoryImplementor))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArguments()[0].hashCode();
}
});
when(mockType.disassemble(anyObject(), eq(sessionImplementor), eq(null))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArguments()[0];
}
});
final NaturalIdCacheKey key = (NaturalIdCacheKey) DefaultCacheKeysFactory.staticCreateNaturalIdKey(new Object[] { "a", "b", "c" }, entityPersister, sessionImplementor);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(key);
final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
final NaturalIdCacheKey keyClone = (NaturalIdCacheKey) ois.readObject();
assertEquals(key, keyClone);
assertEquals(key.hashCode(), keyClone.hashCode());
assertEquals(key.toString(), keyClone.toString());
assertEquals(key.getEntityName(), keyClone.getEntityName());
assertArrayEquals(key.getNaturalIdValues(), keyClone.getNaturalIdValues());
assertEquals(key.getTenantId(), keyClone.getTenantId());
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class MultiPathCircleCascadeTest method testEntityWithNonNullableEntityNull.
private void testEntityWithNonNullableEntityNull(EntityOperation operation) {
Route route = getUpdatedDetachedEntity();
Node node = (Node) route.getNodes().iterator().next();
route.getNodes().remove(node);
node.setRoute(null);
Session s = openSession();
s.beginTransaction();
try {
operation.doEntityOperation(node, s);
s.getTransaction().commit();
fail("should have thrown an exception");
} catch (Exception ex) {
checkExceptionFromNullValueForNonNullable(ex, ((SessionImplementor) s).getFactory().getSettings().isCheckNullability(), true, operation.isLegacy());
} finally {
s.getTransaction().rollback();
s.close();
cleanup();
}
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class JoinFetchProfileTest method testBasicFetchProfileOperation.
@Test
public void testBasicFetchProfileOperation() {
assertTrue("fetch profile not parsed properly", sessionFactory().containsFetchProfileDefinition("enrollment.details"));
assertTrue("fetch profile not parsed properly", sessionFactory().containsFetchProfileDefinition("offering.details"));
assertTrue("fetch profile not parsed properly", sessionFactory().containsFetchProfileDefinition("course.details"));
Session s = openSession();
SessionImplementor si = (SessionImplementor) s;
s.enableFetchProfile("enrollment.details");
assertTrue(si.getLoadQueryInfluencers().hasEnabledFetchProfiles());
s.disableFetchProfile("enrollment.details");
assertFalse(si.getLoadQueryInfluencers().hasEnabledFetchProfiles());
try {
s.enableFetchProfile("never-gonna-get-it");
fail("expecting failure on undefined fetch-profile");
} catch (UnknownProfileException expected) {
}
s.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class SQLExceptionConversionTest method testBadGrammar.
@Test
public void testBadGrammar() throws Exception {
final Session session = openSession();
session.beginTransaction();
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
// prepare/execute a query against a non-existent table
PreparedStatement ps = null;
try {
ps = ((SessionImplementor) session).getJdbcCoordinator().getStatementPreparer().prepareStatement("SELECT user_id, user_name FROM tbl_no_there");
((SessionImplementor) session).getJdbcCoordinator().getResultSetReturn().extract(ps);
fail("SQL compilation should have failed");
} catch (SQLGrammarException ignored) {
// expected outcome
} finally {
releaseStatement(session, ps);
}
}
});
session.getTransaction().rollback();
session.close();
}
Aggregations