Search in sources :

Example 56 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class LoadQueryJoinAndFetchProcessor method renderCollectionJoin.

private void renderCollectionJoin(Join join, JoinFragment joinFragment) {
    final CollectionQuerySpace rightHandSide = (CollectionQuerySpace) join.getRightHandSide();
    // The SQL join to the "collection table" needs to be rendered.
    // 
    // In the case of a basic collection, that's the only join needed.
    // 
    // For one-to-many/many-to-many, we need to render the "collection table join"
    // here (as already stated). There will be a follow-on join (rhs will have a join) for the associated entity.
    // For many-to-many, the follow-on join will join to the associated entity element table. For one-to-many,
    // the collection table is the associated entity table, so the follow-on join will not be rendered..
    // currently we do not explicitly track the joins under the CollectionQuerySpace to know which is
    // the element join and which is the index join (maybe we should?).
    JoinDefinedByMetadata collectionElementJoin = null;
    JoinDefinedByMetadata collectionIndexJoin = null;
    for (Join collectionJoin : rightHandSide.getJoins()) {
        if (JoinDefinedByMetadata.class.isInstance(collectionJoin)) {
            final JoinDefinedByMetadata collectionJoinDefinedByMetadata = (JoinDefinedByMetadata) collectionJoin;
            if (CollectionPropertyNames.COLLECTION_ELEMENTS.equals(collectionJoinDefinedByMetadata.getJoinedPropertyName())) {
                if (collectionElementJoin != null) {
                    throw new AssertionFailure(String.format("More than one element join defined for: %s", rightHandSide.getCollectionPersister().getRole()));
                }
                collectionElementJoin = collectionJoinDefinedByMetadata;
            }
            if (CollectionPropertyNames.COLLECTION_INDICES.equals(collectionJoinDefinedByMetadata.getJoinedPropertyName())) {
                if (collectionIndexJoin != null) {
                    throw new AssertionFailure(String.format("More than one index join defined for: %s", rightHandSide.getCollectionPersister().getRole()));
                }
                collectionIndexJoin = collectionJoinDefinedByMetadata;
            }
        }
    }
    if (rightHandSide.getCollectionPersister().isOneToMany() || rightHandSide.getCollectionPersister().isManyToMany()) {
        // sql aliases to use for the entity.
        if (collectionElementJoin == null) {
            throw new IllegalStateException(String.format("Could not locate collection element join within collection join [%s : %s]", rightHandSide.getUid(), rightHandSide.getCollectionPersister()));
        }
        aliasResolutionContext.generateCollectionReferenceAliases(rightHandSide.getUid(), rightHandSide.getCollectionPersister(), collectionElementJoin.getRightHandSide().getUid());
    } else {
        aliasResolutionContext.generateCollectionReferenceAliases(rightHandSide.getUid(), rightHandSide.getCollectionPersister(), null);
    }
    if (rightHandSide.getCollectionPersister().hasIndex() && rightHandSide.getCollectionPersister().getIndexType().isEntityType()) {
        // sql aliases to use for the entity.
        if (collectionIndexJoin == null) {
            throw new IllegalStateException(String.format("Could not locate collection index join within collection join [%s : %s]", rightHandSide.getUid(), rightHandSide.getCollectionPersister()));
        }
        aliasResolutionContext.generateEntityReferenceAliases(collectionIndexJoin.getRightHandSide().getUid(), rightHandSide.getCollectionPersister().getIndexDefinition().toEntityDefinition().getEntityPersister());
    }
    addJoins(join, joinFragment, (Joinable) rightHandSide.getCollectionPersister(), null);
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) JoinDefinedByMetadata(org.hibernate.loader.plan.spi.JoinDefinedByMetadata) Join(org.hibernate.loader.plan.spi.Join) CollectionQuerySpace(org.hibernate.loader.plan.spi.CollectionQuerySpace)

Aggregations

AssertionFailure (org.hibernate.AssertionFailure)56 Serializable (java.io.Serializable)11 EntityEntry (org.hibernate.engine.spi.EntityEntry)11 Property (org.hibernate.mapping.Property)10 AnnotationException (org.hibernate.AnnotationException)9 PersistentClass (org.hibernate.mapping.PersistentClass)9 SimpleValue (org.hibernate.mapping.SimpleValue)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 SQLException (java.sql.SQLException)6 HashMap (java.util.HashMap)6 HibernateException (org.hibernate.HibernateException)6 PreparedStatement (java.sql.PreparedStatement)5 Column (org.hibernate.mapping.Column)5 Component (org.hibernate.mapping.Component)5 Join (org.hibernate.mapping.Join)5 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)5 Iterator (java.util.Iterator)4 Map (java.util.Map)4 XProperty (org.hibernate.annotations.common.reflection.XProperty)4 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)4