Search in sources :

Example 21 with SessionImplementor

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

the class OrderByTest method testInverseIndex.

@Test
@TestForIssue(jiraKey = "HHH-5732")
public void testInverseIndex() {
    final CollectionPersister transactionsPersister = sessionFactory().getCollectionPersister(BankAccount.class.getName() + ".transactions");
    assertTrue(transactionsPersister.isInverse());
    Session s = openSession();
    s.getTransaction().begin();
    BankAccount account = new BankAccount();
    account.addTransaction("zzzzz");
    account.addTransaction("aaaaa");
    account.addTransaction("mmmmm");
    s.save(account);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.getTransaction().begin();
    try {
        final QueryableCollection queryableCollection = (QueryableCollection) transactionsPersister;
        SimpleSelect select = new SimpleSelect(getDialect()).setTableName(queryableCollection.getTableName()).addColumn("code").addColumn("transactions_index");
        PreparedStatement preparedStatement = ((SessionImplementor) s).getJdbcCoordinator().getStatementPreparer().prepareStatement(select.toStatementString());
        ResultSet resultSet = ((SessionImplementor) s).getJdbcCoordinator().getResultSetReturn().extract(preparedStatement);
        Map<Integer, String> valueMap = new HashMap<Integer, String>();
        while (resultSet.next()) {
            final String code = resultSet.getString(1);
            assertFalse("code column was null", resultSet.wasNull());
            final int indx = resultSet.getInt(2);
            assertFalse("List index column was null", resultSet.wasNull());
            valueMap.put(indx, code);
        }
        assertEquals(3, valueMap.size());
        assertEquals("zzzzz", valueMap.get(0));
        assertEquals("aaaaa", valueMap.get(1));
        assertEquals("mmmmm", valueMap.get(2));
    } catch (SQLException e) {
        fail(e.getMessage());
    } finally {
        s.getTransaction().rollback();
        s.close();
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) PreparedStatement(java.sql.PreparedStatement) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) ResultSet(java.sql.ResultSet) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) SimpleSelect(org.hibernate.sql.SimpleSelect) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 22 with SessionImplementor

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

the class DynamicBatchFetchTest method testDynamicBatchFetch.

@Test
public void testDynamicBatchFetch() {
    Integer aId1 = createAAndB();
    Integer aId2 = createAAndB();
    Session s = openSession();
    s.getTransaction().begin();
    List resultList = s.createQuery("from A where id in (" + aId1 + "," + aId2 + ") order by id").list();
    A a1 = (A) resultList.get(0);
    A a2 = (A) resultList.get(1);
    assertEquals(aId1, a1.getId());
    assertEquals(aId2, a2.getId());
    assertFalse(Hibernate.isInitialized(a1.getB()));
    assertFalse(Hibernate.isInitialized(a2.getB()));
    assertEquals("foo", a1.getB().getOtherProperty());
    assertTrue(Hibernate.isInitialized(a1.getB()));
    // a2.getB() is still uninitialized
    assertFalse(Hibernate.isInitialized(a2.getB()));
    // the B entity has been loaded, but is has not been made the target of a2.getB() yet.
    assertTrue(((SessionImplementor) session).getPersistenceContext().containsEntity(new EntityKey(((SessionImplementor) session).getContextEntityIdentifier(a2.getB()), ((SessionImplementor) session).getFactory().getEntityPersister(B.class.getName()))));
    // a2.getB() is still uninitialized; getting the ID for a2.getB() did not initialize it.
    assertFalse(Hibernate.isInitialized(a2.getB()));
    assertEquals("foo", a2.getB().getOtherProperty());
    // now it's initialized.
    assertTrue(Hibernate.isInitialized(a2.getB()));
    s.getTransaction().commit();
    s.close();
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 23 with SessionImplementor

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

the class CollectionCacheEvictionTest method testCachedValueAfterEviction.

@Test
public void testCachedValueAfterEviction() {
    CollectionPersister persister = sessionFactory().getCollectionPersister(Company.class.getName() + ".users");
    Session session = openSession();
    SessionImplementor sessionImplementor = (SessionImplementor) session;
    CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
    Object key = cache.generateCacheKey(1, persister, sessionFactory(), session.getTenantIdentifier());
    Object cachedValue = cache.get(sessionImplementor, key, sessionImplementor.getTimestamp());
    assertNull(cachedValue);
    Company company = session.get(Company.class, 1);
    //should add in cache
    assertEquals(1, company.getUsers().size());
    session.close();
    session = openSession();
    sessionImplementor = (SessionImplementor) session;
    key = cache.generateCacheKey(1, persister, sessionFactory(), session.getTenantIdentifier());
    cachedValue = cache.get(sessionImplementor, key, sessionImplementor.getTimestamp());
    assertNotNull("Collection wasn't cached", cachedValue);
    session.close();
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 24 with SessionImplementor

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

the class MultiPathCircleCascadeTest method testEntityWithNonNullableTransientEntity.

private void testEntityWithNonNullableTransientEntity(EntityOperation operation) {
    Route route = getUpdatedDetachedEntity();
    Node node = (Node) route.getNodes().iterator().next();
    route.getNodes().remove(node);
    Route routeNew = new Route();
    routeNew.setName("new route");
    routeNew.getNodes().add(node);
    node.setRoute(routeNew);
    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(), false, 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 25 with SessionImplementor

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

the class MultiPathCircleCascadeTest method testEntityWithNonNullablePropSetToNull.

private void testEntityWithNonNullablePropSetToNull(EntityOperation operation) {
    Route route = getUpdatedDetachedEntity();
    Node node = (Node) route.getNodes().iterator().next();
    node.setName(null);
    Session s = openSession();
    s.beginTransaction();
    try {
        operation.doEntityOperation(route, 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)

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