Search in sources :

Example 26 with CollectionType

use of org.hibernate.type.CollectionType 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

CollectionType (org.hibernate.type.CollectionType)26 Type (org.hibernate.type.Type)21 JoinType (org.hibernate.sql.JoinType)10 EntityType (org.hibernate.type.EntityType)10 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)7 AssociationType (org.hibernate.type.AssociationType)7 CompositeType (org.hibernate.type.CompositeType)7 QueryException (org.hibernate.QueryException)5 JoinSequence (org.hibernate.engine.internal.JoinSequence)5 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)5 HibernateException (org.hibernate.HibernateException)3 ComponentType (org.hibernate.type.ComponentType)3 SemanticException (antlr.SemanticException)2 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 MappingException (org.hibernate.MappingException)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 SessionFactoryHelper (org.hibernate.hql.internal.ast.util.SessionFactoryHelper)2 ClassMetadata (org.hibernate.metadata.ClassMetadata)2