Search in sources :

Example 31 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class PersistentListTest method testInverseListIndex2.

@Test
@TestForIssue(jiraKey = "HHH-5732")
public void testInverseListIndex2() {
    // make sure no one changes the mapping
    final CollectionPersister collectionPersister = sessionFactory().getCollectionPersister(Order.class.getName() + ".lineItems");
    assertTrue(collectionPersister.isInverse());
    // do some creations...
    Session session = openSession();
    session.beginTransaction();
    Order order = new Order("acme-1");
    order.addLineItem("abc", 2);
    order.addLineItem("def", 200);
    order.addLineItem("ghi", 13);
    session.save(order);
    session.getTransaction().commit();
    session.close();
    // now, make sure the list-index column gotten written...
    final Session session2 = openSession();
    session2.beginTransaction();
    session2.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            final QueryableCollection queryableCollection = (QueryableCollection) collectionPersister;
            SimpleSelect select = new SimpleSelect(getDialect()).setTableName(queryableCollection.getTableName()).addColumn("ORDER_ID").addColumn("INDX").addColumn("PRD_CODE");
            PreparedStatement preparedStatement = ((SessionImplementor) session2).getJdbcCoordinator().getStatementPreparer().prepareStatement(select.toStatementString());
            ResultSet resultSet = ((SessionImplementor) session2).getJdbcCoordinator().getResultSetReturn().extract(preparedStatement);
            Map<String, Integer> valueMap = new HashMap<String, Integer>();
            while (resultSet.next()) {
                final int fk = resultSet.getInt(1);
                assertFalse("Collection key (FK) column was null", resultSet.wasNull());
                final int indx = resultSet.getInt(2);
                assertFalse("List index column was null", resultSet.wasNull());
                final String prodCode = resultSet.getString(3);
                assertFalse("Prod code column was null", resultSet.wasNull());
                valueMap.put(prodCode, indx);
            }
            assertEquals(3, valueMap.size());
            assertEquals(Integer.valueOf(0), valueMap.get("abc"));
            assertEquals(Integer.valueOf(1), valueMap.get("def"));
            assertEquals(Integer.valueOf(2), valueMap.get("ghi"));
        }
    });
    session2.delete(order);
    session2.getTransaction().commit();
    session2.close();
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) PreparedStatement(java.sql.PreparedStatement) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Work(org.hibernate.jdbc.Work) ResultSet(java.sql.ResultSet) SimpleSelect(org.hibernate.sql.SimpleSelect) HashMap(java.util.HashMap) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 32 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class DynamicFilterTest method testSecondLevelCachedCollectionsFiltering.

@Test
public void testSecondLevelCachedCollectionsFiltering() {
    TestData testData = new TestData();
    testData.prepare();
    Session session = openSession();
    long ts = ((SessionImplementor) session).getTimestamp();
    // Force a collection into the second level cache, with its non-filtered elements
    Salesperson sp = (Salesperson) session.load(Salesperson.class, testData.steveId);
    Hibernate.initialize(sp.getOrders());
    CollectionPersister persister = sessionFactory().getCollectionPersister(Salesperson.class.getName() + ".orders");
    assertTrue("No cache for collection", persister.hasCache());
    CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
    Object cacheKey = cache.generateCacheKey(testData.steveId, persister, sessionFactory(), session.getTenantIdentifier());
    CollectionCacheEntry cachedData = (CollectionCacheEntry) cache.get((SessionImplementor) session, cacheKey, ts);
    assertNotNull("collection was not in cache", cachedData);
    session.close();
    session = openSession();
    ts = ((SessionImplementor) session).getTimestamp();
    session.enableFilter("fulfilledOrders").setParameter("asOfDate", testData.lastMonth.getTime());
    sp = (Salesperson) session.createQuery("from Salesperson as s where s.id = :id").setLong("id", testData.steveId).uniqueResult();
    assertEquals("Filtered-collection not bypassing 2L-cache", 1, sp.getOrders().size());
    Object cacheKey2 = cache.generateCacheKey(testData.steveId, persister, sessionFactory(), session.getTenantIdentifier());
    CollectionCacheEntry cachedData2 = (CollectionCacheEntry) persister.getCacheAccessStrategy().get((SessionImplementor) session, cacheKey2, ts);
    assertNotNull("collection no longer in cache!", cachedData2);
    assertSame("Different cache values!", cachedData, cachedData2);
    session.close();
    session = openSession();
    session.enableFilter("fulfilledOrders").setParameter("asOfDate", testData.lastMonth.getTime());
    sp = (Salesperson) session.load(Salesperson.class, testData.steveId);
    assertEquals("Filtered-collection not bypassing 2L-cache", 1, sp.getOrders().size());
    session.close();
    // Finally, make sure that the original cached version did not get over-written
    session = openSession();
    sp = (Salesperson) session.load(Salesperson.class, testData.steveId);
    assertEquals("Actual cached version got over-written", 2, sp.getOrders().size());
    session.close();
    testData.release();
}
Also used : CollectionCacheEntry(org.hibernate.cache.spi.entry.CollectionCacheEntry) 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 33 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister 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);
    CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
    Object key = cache.generateCacheKey(id, persister, sessionFactory(), session.getTenantIdentifier());
    Object cachedValue = cache.get(((SessionImplementor) session), key, ((SessionImplementor) session).getTimestamp());
    session.close();
    return cachedValue != null;
}
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)

Example 34 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class DefaultRefreshEventListener method evictCachedCollections.

private void evictCachedCollections(Type[] types, Serializable id, EventSource source) throws HibernateException {
    for (Type type : types) {
        if (type.isCollectionType()) {
            CollectionPersister collectionPersister = source.getFactory().getMetamodel().collectionPersister(((CollectionType) type).getRole());
            if (collectionPersister.hasCache()) {
                final CollectionRegionAccessStrategy cache = collectionPersister.getCacheAccessStrategy();
                final Object ck = cache.generateCacheKey(id, collectionPersister, source.getFactory(), source.getTenantIdentifier());
                final SoftLock lock = cache.lockItem(source, ck, null);
                source.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() {

                    @Override
                    public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
                        cache.unlockItem(session, ck, lock);
                    }
                });
                cache.remove(source, ck);
            }
        } else if (type.isComponentType()) {
            CompositeType actype = (CompositeType) type;
            evictCachedCollections(actype.getSubtypes(), id, source);
        }
    }
}
Also used : CollectionType(org.hibernate.type.CollectionType) CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) AfterTransactionCompletionProcess(org.hibernate.action.spi.AfterTransactionCompletionProcess) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy) SoftLock(org.hibernate.cache.spi.access.SoftLock) CompositeType(org.hibernate.type.CompositeType)

Example 35 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister in project hibernate-orm by hibernate.

the class BatchFetchQueue method addBatchLoadableCollection.

// collection batch support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
	 * If an CollectionEntry represents a batch loadable collection, add
	 * it to the queue.
	 */
public void addBatchLoadableCollection(PersistentCollection collection, CollectionEntry ce) {
    final CollectionPersister persister = ce.getLoadedPersister();
    LinkedHashMap<CollectionEntry, PersistentCollection> map = batchLoadableCollections.get(persister.getRole());
    if (map == null) {
        map = new LinkedHashMap<>(16);
        batchLoadableCollections.put(persister.getRole(), map);
    }
    map.put(ce, collection);
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) CollectionPersister(org.hibernate.persister.collection.CollectionPersister)

Aggregations

CollectionPersister (org.hibernate.persister.collection.CollectionPersister)44 HibernateException (org.hibernate.HibernateException)11 Type (org.hibernate.type.Type)9 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)8 Serializable (java.io.Serializable)7 CollectionRegionAccessStrategy (org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)7 CollectionType (org.hibernate.type.CollectionType)7 HashMap (java.util.HashMap)6 Session (org.hibernate.Session)6 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)6 Test (org.junit.Test)6 Map (java.util.Map)5 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)5 EntityEntry (org.hibernate.engine.spi.EntityEntry)5 CompositeType (org.hibernate.type.CompositeType)5 EntityType (org.hibernate.type.EntityType)5 ResultSet (java.sql.ResultSet)4 AssertionFailure (org.hibernate.AssertionFailure)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4