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