Search in sources :

Example 11 with Joinable

use of org.hibernate.persister.entity.Joinable in project hibernate-orm by hibernate.

the class AbstractLoadPlanBuildingAssociationVisitationStrategy method startingEntity.

// Entities  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public void startingEntity(EntityDefinition entityDefinition) {
    // see if the EntityDefinition is a root...
    final boolean isRoot = fetchSourceStack.isEmpty();
    if (!isRoot) {
        // if not, this call should represent a fetch which should have been handled in #startingAttribute
        return;
    }
    // if we get here, it is a root
    log.tracef("%s Starting root entity : %s", StringHelper.repeat(">>", fetchSourceStack.size()), entityDefinition.getEntityPersister().getEntityName());
    if (!supportsRootEntityReturns()) {
        throw new HibernateException("This strategy does not support root entity returns");
    }
    final EntityReturnImpl entityReturn = new EntityReturnImpl(entityDefinition, querySpaces);
    addRootReturn(entityReturn);
    pushToStack(entityReturn);
    // also add an AssociationKey for the root so we can later on recognize circular references back to the root.
    final Joinable entityPersister = (Joinable) entityDefinition.getEntityPersister();
    associationKeyRegistered(new AssociationKey(entityPersister.getTableName(), entityPersister.getKeyColumnNames()));
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) HibernateException(org.hibernate.HibernateException) Joinable(org.hibernate.persister.entity.Joinable) EntityReturnImpl(org.hibernate.loader.plan.build.internal.returns.EntityReturnImpl)

Example 12 with Joinable

use of org.hibernate.persister.entity.Joinable in project hibernate-orm by hibernate.

the class LoadQueryJoinAndFetchProcessor method renderEntityJoin.

private void renderEntityJoin(Join join, JoinFragment joinFragment) {
    final EntityQuerySpace rightHandSide = (EntityQuerySpace) join.getRightHandSide();
    // see if there is already aliases registered for this entity query space (collection joins)
    EntityReferenceAliases aliases = aliasResolutionContext.resolveEntityReferenceAliases(rightHandSide.getUid());
    if (aliases == null) {
        aliasResolutionContext.generateEntityReferenceAliases(rightHandSide.getUid(), rightHandSide.getEntityPersister());
    }
    final Joinable joinable = (Joinable) rightHandSide.getEntityPersister();
    addJoins(join, joinFragment, joinable, null);
}
Also used : EntityReferenceAliases(org.hibernate.loader.plan.exec.spi.EntityReferenceAliases) Joinable(org.hibernate.persister.entity.Joinable) EntityQuerySpace(org.hibernate.loader.plan.spi.EntityQuerySpace)

Example 13 with Joinable

use of org.hibernate.persister.entity.Joinable in project hibernate-orm by hibernate.

the class EntityBasedAssociationAttribute method getAssociationKey.

@Override
public AssociationKey getAssociationKey() {
    final AssociationType type = getType();
    if (type.isAnyType()) {
        return new AssociationKey(JoinHelper.getLHSTableName(type, attributeNumber(), (OuterJoinLoadable) getSource()), JoinHelper.getLHSColumnNames(type, attributeNumber(), 0, (OuterJoinLoadable) getSource(), sessionFactory()));
    }
    final Joinable joinable = type.getAssociatedJoinable(sessionFactory());
    if (type.getForeignKeyDirection() == ForeignKeyDirection.FROM_PARENT) {
        final String lhsTableName;
        final String[] lhsColumnNames;
        if (joinable.isCollection()) {
            final QueryableCollection collectionPersister = (QueryableCollection) joinable;
            lhsTableName = collectionPersister.getTableName();
            lhsColumnNames = collectionPersister.getElementColumnNames();
        } else {
            final OuterJoinLoadable entityPersister = (OuterJoinLoadable) source();
            lhsTableName = getLHSTableName(type, attributeNumber(), entityPersister);
            lhsColumnNames = getLHSColumnNames(type, attributeNumber(), entityPersister, sessionFactory());
        }
        return new AssociationKey(lhsTableName, lhsColumnNames);
    } else {
        return new AssociationKey(joinable.getTableName(), getRHSColumnNames(type, sessionFactory()));
    }
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 14 with Joinable

use of org.hibernate.persister.entity.Joinable in project hibernate-orm by hibernate.

the class AttributeNodeImpl method internalMakeSubgraph.

@SuppressWarnings("unchecked")
private <X> SubgraphImpl<X> internalMakeSubgraph(Class<X> type) {
    if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.BASIC || attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED) {
        throw new IllegalArgumentException(String.format("Attribute [%s] is not of managed type", getAttributeName()));
    }
    if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ELEMENT_COLLECTION) {
        throw new IllegalArgumentException(String.format("Collection elements [%s] is not of managed type", getAttributeName()));
    }
    if (subgraphMap == null) {
        subgraphMap = new HashMap<>();
    }
    final Helper.AttributeSource attributeSource = Helper.resolveAttributeSource(sessionFactory(), managedType);
    final AssociationType attributeType = (AssociationType) attributeSource.findType(attribute.getName());
    final Joinable joinable = attributeType.getAssociatedJoinable(sessionFactory());
    if (joinable.isCollection()) {
        final EntityPersister elementEntityPersister = ((QueryableCollection) joinable).getElementPersister();
        if (type == null) {
            type = elementEntityPersister.getMappedClass();
        } else {
            if (!isTreatableAs(elementEntityPersister, type)) {
                throw new IllegalArgumentException(String.format("Collection elements [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), elementEntityPersister.getMappedClass().getName()));
            }
        }
    } else {
        final EntityPersister entityPersister = (EntityPersister) joinable;
        if (type == null) {
            type = entityPersister.getMappedClass();
        } else {
            if (!isTreatableAs(entityPersister, type)) {
                throw new IllegalArgumentException(String.format("Attribute [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), entityPersister.getMappedClass().getName()));
            }
        }
    }
    ManagedType managedType = null;
    try {
        managedType = sessionFactory.getMetamodel().entity(type.getName());
    } catch (IllegalArgumentException e) {
    // do nothing
    }
    if (managedType == null) {
        managedType = attribute.getDeclaringType();
    }
    final SubgraphImpl<X> subgraph = new SubgraphImpl<>(this.sessionFactory, managedType, type);
    subgraphMap.put(type, subgraph);
    return subgraph;
}
Also used : CollectionHelper(org.hibernate.internal.util.collections.CollectionHelper) Helper(org.hibernate.metamodel.internal.Helper) EntityPersister(org.hibernate.persister.entity.EntityPersister) ManagedType(javax.persistence.metamodel.ManagedType) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 15 with Joinable

use of org.hibernate.persister.entity.Joinable 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

Joinable (org.hibernate.persister.entity.Joinable)19 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)6 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)6 AssociationKey (org.hibernate.persister.walking.spi.AssociationKey)5 AssociationType (org.hibernate.type.AssociationType)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 HibernateException (org.hibernate.HibernateException)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 EntityReferenceInitializerImpl (org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl)2 EntityReferenceAliases (org.hibernate.loader.plan.exec.spi.EntityReferenceAliases)2 FetchSource (org.hibernate.loader.plan.spi.FetchSource)2 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 CollectionType (org.hibernate.type.CollectionType)2 SetType (org.hibernate.type.SetType)2 AST (antlr.collections.AST)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1