Search in sources :

Example 41 with SessionImplementor

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();
}
Also used : Transaction(org.hibernate.Transaction) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test)

Example 42 with SessionImplementor

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());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NaturalIdCacheKey(org.hibernate.cache.internal.NaturalIdCacheKey) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Type(org.hibernate.type.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Matchers.anyObject(org.mockito.Matchers.anyObject) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 43 with SessionImplementor

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();
    }
}
Also used : SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PropertyValueException(org.hibernate.PropertyValueException) TransientPropertyValueException(org.hibernate.TransientPropertyValueException) PersistenceException(javax.persistence.PersistenceException) JDBCException(org.hibernate.JDBCException) Session(org.hibernate.Session)

Example 44 with SessionImplementor

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();
}
Also used : UnknownProfileException(org.hibernate.UnknownProfileException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test)

Example 45 with SessionImplementor

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();
}
Also used : SQLGrammarException(org.hibernate.exception.SQLGrammarException) SQLException(java.sql.SQLException) Work(org.hibernate.jdbc.Work) Connection(java.sql.Connection) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PreparedStatement(java.sql.PreparedStatement) 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