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 dhis2-core by dhis2.
the class AbstractPropertyIntrospectorService method getPropertiesFromHibernate.
protected Map<String, Property> getPropertiesFromHibernate(Class<?> klass) {
updateJoinTables();
ClassMetadata classMetadata = sessionFactory.getClassMetadata(klass);
// is class persisted with hibernate
if (classMetadata == null) {
return new HashMap<>();
}
Map<String, Property> properties = new HashMap<>();
SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
MetadataImplementor metadataImplementor = HibernateMetadata.getMetadataImplementor();
if (metadataImplementor == null) {
return new HashMap<>();
}
PersistentClass persistentClass = metadataImplementor.getEntityBinding(klass.getName());
Iterator<?> propertyIterator = persistentClass.getPropertyClosureIterator();
while (propertyIterator.hasNext()) {
Property property = new Property(klass);
property.setRequired(false);
property.setPersisted(true);
property.setOwner(true);
org.hibernate.mapping.Property hibernateProperty = (org.hibernate.mapping.Property) propertyIterator.next();
Type type = hibernateProperty.getType();
property.setName(hibernateProperty.getName());
property.setCascade(hibernateProperty.getCascade());
property.setCollection(type.isCollectionType());
property.setSetterMethod(hibernateProperty.getSetter(klass).getMethod());
property.setGetterMethod(hibernateProperty.getGetter(klass).getMethod());
if (property.isCollection()) {
CollectionType collectionType = (CollectionType) type;
CollectionPersister persister = sessionFactoryImplementor.getCollectionPersister(collectionType.getRole());
property.setOwner(!persister.isInverse());
property.setManyToMany(persister.isManyToMany());
property.setMin(0d);
property.setMax(Double.MAX_VALUE);
if (property.isOwner()) {
property.setOwningRole(collectionType.getRole());
property.setInverseRole(roleToRole.get(collectionType.getRole()));
} else {
property.setOwningRole(roleToRole.get(collectionType.getRole()));
property.setInverseRole(collectionType.getRole());
}
}
if (SingleColumnType.class.isInstance(type) || CustomType.class.isInstance(type) || ManyToOneType.class.isInstance(type)) {
Column column = (Column) hibernateProperty.getColumnIterator().next();
property.setUnique(column.isUnique());
property.setRequired(!column.isNullable());
property.setMin(0d);
property.setMax((double) column.getLength());
property.setLength(column.getLength());
if (TextType.class.isInstance(type)) {
property.setMin(0d);
property.setMax((double) Integer.MAX_VALUE);
property.setLength(Integer.MAX_VALUE);
} else if (IntegerType.class.isInstance(type)) {
property.setMin(0d);
property.setMax((double) Integer.MAX_VALUE);
property.setLength(Integer.MAX_VALUE);
} else if (LongType.class.isInstance(type)) {
property.setMin(0d);
property.setMax((double) Long.MAX_VALUE);
property.setLength(Integer.MAX_VALUE);
} else if (DoubleType.class.isInstance(type)) {
property.setMin(0d);
property.setMax(Double.MAX_VALUE);
property.setLength(Integer.MAX_VALUE);
}
}
if (ManyToOneType.class.isInstance(type)) {
property.setManyToOne(true);
property.setRequired(property.isRequired() && !property.isCollection());
if (property.isOwner()) {
property.setOwningRole(klass.getName() + "." + property.getName());
} else {
property.setInverseRole(klass.getName() + "." + property.getName());
}
} else if (OneToOneType.class.isInstance(type)) {
property.setOneToOne(true);
}
properties.put(property.getName(), property);
}
return properties;
}
use of org.hibernate.persister.collection.CollectionPersister in project dhis2-core by dhis2.
the class AbstractPropertyIntrospectorService method updateJoinTables.
protected void updateJoinTables() {
if (!roleToRole.isEmpty()) {
return;
}
Map<String, List<String>> joinTableToRoles = new HashMap<>();
SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
Iterator<?> collectionIterator = sessionFactory.getAllCollectionMetadata().values().iterator();
while (collectionIterator.hasNext()) {
CollectionPersister collectionPersister = (CollectionPersister) collectionIterator.next();
CollectionType collectionType = collectionPersister.getCollectionType();
if (collectionPersister.isManyToMany() && collectionType.isAssociationType()) {
Joinable associatedJoinable = collectionType.getAssociatedJoinable(sessionFactoryImplementor);
if (!joinTableToRoles.containsKey(associatedJoinable.getTableName())) {
joinTableToRoles.put(associatedJoinable.getTableName(), new ArrayList<>());
}
joinTableToRoles.get(associatedJoinable.getTableName()).add(collectionPersister.getRole());
} else if (collectionPersister.isInverse()) {
if (SetType.class.isInstance(collectionType)) {
SetType setType = (SetType) collectionType;
setType.getAssociatedJoinable(sessionFactoryImplementor);
}
}
}
Iterator<Map.Entry<String, List<String>>> entryIterator = joinTableToRoles.entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<String, List<String>> entry = entryIterator.next();
if (entry.getValue().size() < 2) {
entryIterator.remove();
}
}
for (Map.Entry<String, List<String>> entry : joinTableToRoles.entrySet()) {
roleToRole.put(entry.getValue().get(0), entry.getValue().get(1));
roleToRole.put(entry.getValue().get(1), entry.getValue().get(0));
}
}
Aggregations