Search in sources :

Example 1 with AssociationType

use of org.hibernate.type.AssociationType in project hibernate-orm by hibernate.

the class PathExpressionParser method addFromCollection.

public String addFromCollection(QueryTranslatorImpl q) throws QueryException {
    Type collectionElementType = getPropertyType();
    if (collectionElementType == null) {
        throw new QueryException("must specify 'elements' for collection valued property in from clause: " + path);
    }
    if (collectionElementType.isEntityType()) {
        // an association
        QueryableCollection collectionPersister = q.getCollectionPersister(collectionRole);
        Queryable entityPersister = (Queryable) collectionPersister.getElementPersister();
        String clazz = entityPersister.getEntityName();
        final String elementName;
        if (collectionPersister.isOneToMany()) {
            elementName = collectionName;
            //allow index() function:
            q.decoratePropertyMapping(elementName, collectionPersister);
        } else {
            //many-to-many
            q.addCollection(collectionName, collectionRole);
            elementName = q.createNameFor(clazz);
            addJoin(elementName, (AssociationType) collectionElementType);
        }
        q.addFrom(elementName, clazz, joinSequence);
        currentPropertyMapping = new CollectionPropertyMapping(collectionPersister);
        return elementName;
    } else {
        // collections of values
        q.addFromCollection(collectionName, collectionRole, joinSequence);
        return collectionName;
    }
}
Also used : JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) Queryable(org.hibernate.persister.entity.Queryable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) CollectionPropertyMapping(org.hibernate.persister.collection.CollectionPropertyMapping)

Example 2 with AssociationType

use of org.hibernate.type.AssociationType in project hibernate-orm by hibernate.

the class JoinWalker method walkCompositeElementTree.

/**
	 * For a composite element, add to a list of associations to be fetched by outerjoin
	 */
private void walkCompositeElementTree(final CompositeType compositeType, final String[] cols, final QueryableCollection persister, final String alias, final PropertyPath path, final int currentDepth) throws MappingException {
    Type[] types = compositeType.getSubtypes();
    String[] propertyNames = compositeType.getPropertyNames();
    int begin = 0;
    for (int i = 0; i < types.length; i++) {
        int length = types[i].getColumnSpan(getFactory());
        String[] lhsColumns = ArrayHelper.slice(cols, begin, length);
        if (types[i].isAssociationType()) {
            AssociationType associationType = (AssociationType) types[i];
            // simple, because we can't have a one-to-one or a collection 
            // (or even a property-ref) in a composite-element:
            String[] aliasedLhsColumns = StringHelper.qualify(alias, lhsColumns);
            final PropertyPath subPath = path.append(propertyNames[i]);
            final boolean[] propertyNullability = compositeType.getPropertyNullability();
            final JoinType joinType = getJoinType(associationType, compositeType.getFetchMode(i), subPath, persister.getTableName(), lhsColumns, propertyNullability == null || propertyNullability[i], currentDepth, compositeType.getCascadeStyle(i));
            addAssociationToJoinTreeIfNecessary(associationType, aliasedLhsColumns, alias, subPath, currentDepth, joinType);
        } else if (types[i].isComponentType()) {
            final PropertyPath subPath = path.append(propertyNames[i]);
            walkCompositeElementTree((CompositeType) types[i], lhsColumns, persister, alias, subPath, currentDepth);
        }
        begin += length;
    }
}
Also used : EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) JoinType(org.hibernate.sql.JoinType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) JoinType(org.hibernate.sql.JoinType) CompositeType(org.hibernate.type.CompositeType)

Example 3 with AssociationType

use of org.hibernate.type.AssociationType in project hibernate-orm by hibernate.

the class JoinWalker method walkComponentTree.

/**
	 * For a component, add to a list of associations to be fetched by outerjoin
	 *
	 * @param componentType The component type to be walked.
	 * @param propertyNumber The property number for the component property (relative to
	 * persister).
	 * @param begin todo unknowm
	 * @param persister The owner of the component property
	 * @param alias The root alias
	 * @param path The property access path
	 * @param currentDepth The current join depth
	 *
	 * @throws org.hibernate.MappingException ???
	 */
private void walkComponentTree(final CompositeType componentType, final int propertyNumber, int begin, final OuterJoinLoadable persister, final String alias, final PropertyPath path, final int currentDepth) throws MappingException {
    Type[] types = componentType.getSubtypes();
    String[] propertyNames = componentType.getPropertyNames();
    for (int i = 0; i < types.length; i++) {
        if (types[i].isAssociationType()) {
            AssociationType associationType = (AssociationType) types[i];
            String[] aliasedLhsColumns = JoinHelper.getAliasedLHSColumnNames(associationType, alias, propertyNumber, begin, persister, getFactory());
            String[] lhsColumns = JoinHelper.getLHSColumnNames(associationType, propertyNumber, begin, persister, getFactory());
            String lhsTable = JoinHelper.getLHSTableName(associationType, propertyNumber, persister);
            final PropertyPath subPath = path.append(propertyNames[i]);
            final boolean[] propertyNullability = componentType.getPropertyNullability();
            final JoinType joinType = getJoinType(persister, subPath, propertyNumber, associationType, componentType.getFetchMode(i), componentType.getCascadeStyle(i), lhsTable, lhsColumns, propertyNullability == null || propertyNullability[i], currentDepth);
            addAssociationToJoinTreeIfNecessary(associationType, aliasedLhsColumns, alias, subPath, currentDepth, joinType);
        } else if (types[i].isComponentType()) {
            final PropertyPath subPath = path.append(propertyNames[i]);
            walkComponentTree((CompositeType) types[i], propertyNumber, begin, persister, alias, subPath, currentDepth);
        }
        begin += types[i].getColumnSpan(getFactory());
    }
}
Also used : EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) JoinType(org.hibernate.sql.JoinType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) JoinType(org.hibernate.sql.JoinType) CompositeType(org.hibernate.type.CompositeType)

Example 4 with AssociationType

use of org.hibernate.type.AssociationType in project hibernate-orm by hibernate.

the class AttributeNodeImpl method internalMakeKeySubgraph.

@SuppressWarnings("unchecked")
private <X> SubgraphImpl<X> internalMakeKeySubgraph(Class<X> type) {
    if (!attribute.isCollection()) {
        throw new IllegalArgumentException(String.format("Non-collection attribute [%s] cannot be target of key subgraph", getAttributeName()));
    }
    final PluralAttributeImpl pluralAttribute = (PluralAttributeImpl) attribute;
    if (pluralAttribute.getCollectionType() != PluralAttribute.CollectionType.MAP) {
        throw new IllegalArgumentException(String.format("Non-Map attribute [%s] cannot be target of key subgraph", getAttributeName()));
    }
    final AssociationType attributeType = (AssociationType) Helper.resolveType(sessionFactory(), attribute);
    final QueryableCollection collectionPersister = (QueryableCollection) attributeType.getAssociatedJoinable(sessionFactory());
    final Type indexType = collectionPersister.getIndexType();
    if (!indexType.isAssociationType()) {
        throw new IllegalArgumentException(String.format("Map index [%s] is not an entity; cannot be target of key subgraph", getAttributeName()));
    }
    if (keySubgraphMap == null) {
        keySubgraphMap = new HashMap<>();
    }
    final AssociationType indexAssociationType = (AssociationType) indexType;
    final EntityPersister indexEntityPersister = (EntityPersister) indexAssociationType.getAssociatedJoinable(sessionFactory());
    if (type == null) {
        type = indexEntityPersister.getMappedClass();
    } else {
        if (!isTreatableAs(indexEntityPersister, type)) {
            throw new IllegalArgumentException(String.format("Map key [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), indexEntityPersister.getMappedClass().getName()));
        }
    }
    final SubgraphImpl<X> subgraph = new SubgraphImpl<>(this.sessionFactory, attribute.getDeclaringType(), type);
    keySubgraphMap.put(type, subgraph);
    return subgraph;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ManagedType(javax.persistence.metamodel.ManagedType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) PluralAttributeImpl(org.hibernate.metamodel.internal.PluralAttributeImpl) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 5 with AssociationType

use of org.hibernate.type.AssociationType in project hibernate-orm by hibernate.

the class BasicCollectionJoinWalker method initStatementString.

private void initStatementString(final String alias, final int batchSize, final String subquery) throws MappingException {
    final int joins = countEntityPersisters(associations);
    final int collectionJoins = countCollectionPersisters(associations) + 1;
    suffixes = BasicLoader.generateSuffixes(joins);
    collectionSuffixes = BasicLoader.generateSuffixes(joins, collectionJoins);
    StringBuilder whereString = whereString(alias, collectionPersister.getKeyColumnNames(), subquery, batchSize);
    String manyToManyOrderBy = "";
    String filter = collectionPersister.filterFragment(alias, getLoadQueryInfluencers().getEnabledFilters());
    if (collectionPersister.isManyToMany()) {
        // from the collection of associations, locate OJA for the
        // ManyToOne corresponding to this persister to fully
        // define the many-to-many; we need that OJA so that we can
        // use its alias here
        // TODO : is there a better way here?
        Iterator itr = associations.iterator();
        AssociationType associationType = (AssociationType) collectionPersister.getElementType();
        while (itr.hasNext()) {
            OuterJoinableAssociation oja = (OuterJoinableAssociation) itr.next();
            if (oja.getJoinableType() == associationType) {
                // we found it
                filter += collectionPersister.getManyToManyFilterFragment(oja.getRHSAlias(), getLoadQueryInfluencers().getEnabledFilters());
                manyToManyOrderBy += collectionPersister.getManyToManyOrderByString(oja.getRHSAlias());
            }
        }
    }
    whereString.insert(0, StringHelper.moveAndToBeginning(filter));
    JoinFragment ojf = mergeOuterJoins(associations);
    Select select = new Select(getDialect()).setSelectClause(collectionPersister.selectFragment(alias, collectionSuffixes[0]) + selectString(associations)).setFromClause(collectionPersister.getTableName(), alias).setWhereClause(whereString.toString()).setOuterJoins(ojf.toFromFragmentString(), ojf.toWhereFragmentString());
    select.setOrderByClause(orderBy(associations, mergeOrderings(collectionPersister.getSQLOrderByString(alias), manyToManyOrderBy)));
    if (getFactory().getSettings().isCommentsEnabled()) {
        select.setComment("load collection " + collectionPersister.getRole());
    }
    sql = select.toStatementString();
}
Also used : AssociationType(org.hibernate.type.AssociationType) Iterator(java.util.Iterator) Select(org.hibernate.sql.Select) JoinFragment(org.hibernate.sql.JoinFragment) OuterJoinableAssociation(org.hibernate.loader.OuterJoinableAssociation)

Aggregations

AssociationType (org.hibernate.type.AssociationType)43 Type (org.hibernate.type.Type)18 FetchStyle (org.hibernate.engine.FetchStyle)17 FetchTiming (org.hibernate.engine.FetchTiming)17 Test (org.junit.Test)17 CompositeType (org.hibernate.type.CompositeType)12 EntityType (org.hibernate.type.EntityType)10 JoinType (org.hibernate.sql.JoinType)9 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)5 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)5 UniqueKeyLoadable (org.hibernate.persister.entity.UniqueKeyLoadable)4 CollectionType (org.hibernate.type.CollectionType)4 VersionType (org.hibernate.type.VersionType)4 Iterator (java.util.Iterator)3 ManagedType (javax.persistence.metamodel.ManagedType)3 QueryException (org.hibernate.QueryException)3 Joinable (org.hibernate.persister.entity.Joinable)3 AssociationKey (org.hibernate.persister.walking.spi.AssociationKey)3 Serializable (java.io.Serializable)2