Search in sources :

Example 41 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 42 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 43 with CollectionPersister

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;
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) HashMap(java.util.HashMap) ManyToOneType(org.hibernate.type.ManyToOneType) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) IntegerType(org.hibernate.type.IntegerType) CustomType(org.hibernate.type.CustomType) CollectionType(org.hibernate.type.CollectionType) ManyToOneType(org.hibernate.type.ManyToOneType) IntegerType(org.hibernate.type.IntegerType) TextType(org.hibernate.type.TextType) LongType(org.hibernate.type.LongType) SingleColumnType(org.hibernate.type.SingleColumnType) OneToOneType(org.hibernate.type.OneToOneType) SetType(org.hibernate.type.SetType) DoubleType(org.hibernate.type.DoubleType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Column(org.hibernate.mapping.Column) DoubleType(org.hibernate.type.DoubleType) CollectionType(org.hibernate.type.CollectionType) PersistentClass(org.hibernate.mapping.PersistentClass) OneToOneType(org.hibernate.type.OneToOneType)

Example 44 with CollectionPersister

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));
    }
}
Also used : HashMap(java.util.HashMap) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SetType(org.hibernate.type.SetType) CollectionType(org.hibernate.type.CollectionType) Joinable(org.hibernate.persister.entity.Joinable) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

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