Search in sources :

Example 26 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class DotNode method dereferenceCollection.

private void dereferenceCollection(CollectionType collectionType, boolean implicitJoin, boolean indexed, String classAlias, AST parent) throws SemanticException {
    dereferenceType = DereferenceType.COLLECTION;
    String role = collectionType.getRole();
    //foo.bars.size (also handles deprecated stuff like foo.bars.maxelement for backwardness)
    boolean isSizeProperty = getNextSibling() != null && CollectionProperties.isAnyCollectionProperty(getNextSibling().getText());
    if (isSizeProperty) {
        //yuck!
        indexed = true;
    }
    QueryableCollection queryableCollection = getSessionFactoryHelper().requireQueryableCollection(role);
    String propName = getPath();
    FromClause currentFromClause = getWalker().getCurrentFromClause();
    // If the lhs of the join is a "component join", we need to go back to the
    // first non-component-join as the origin to properly link aliases and
    // join columns
    FromElement lhsFromElement = getLhs().getFromElement();
    while (lhsFromElement != null && ComponentJoin.class.isInstance(lhsFromElement)) {
        lhsFromElement = lhsFromElement.getOrigin();
    }
    if (lhsFromElement == null) {
        throw new QueryException("Unable to locate appropriate lhs");
    }
    // in all other cases, we should use the table alias
    if (getWalker().getStatementType() != SqlTokenTypes.SELECT) {
        if (isFromElementUpdateOrDeleteRoot(lhsFromElement)) {
            // at this point we know we have the 2 conditions above,
            // lets see if we have the mentioned "multi-table" caveat...
            boolean useAlias = false;
            if (getWalker().getStatementType() != SqlTokenTypes.INSERT) {
                final Queryable persister = lhsFromElement.getQueryable();
                if (persister.isMultiTable()) {
                    useAlias = true;
                }
            }
            if (!useAlias) {
                final String lhsTableName = lhsFromElement.getQueryable().getTableName();
                columns = getFromElement().toColumns(lhsTableName, propertyPath, false, true);
            }
        }
    }
    // We do not look for an existing join on the same path, because
    // it makes sense to join twice on the same collection role
    FromElementFactory factory = new FromElementFactory(currentFromClause, lhsFromElement, propName, classAlias, getColumns(), implicitJoin);
    FromElement elem = factory.createCollection(queryableCollection, role, joinType, fetch, indexed);
    LOG.debugf("dereferenceCollection() : Created new FROM element for %s : %s", propName, elem);
    setImpliedJoin(elem);
    // This 'dot' expression now refers to the resulting from element.
    setFromElement(elem);
    if (isSizeProperty) {
        elem.setText("");
        elem.setUseWhereFragment(false);
    }
    if (!implicitJoin) {
        EntityPersister entityPersister = elem.getEntityPersister();
        if (entityPersister != null) {
            getWalker().addQuerySpaces(entityPersister.getQuerySpaces());
        }
    }
    // Always add the collection's query spaces.
    getWalker().addQuerySpaces(queryableCollection.getCollectionSpaces());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) QueryException(org.hibernate.QueryException) Queryable(org.hibernate.persister.entity.Queryable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 27 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class FromElement method buildTypeDiscriminatorMetadata.

private TypeDiscriminatorMetadata buildTypeDiscriminatorMetadata() {
    final String aliasToUse = getTableAlias();
    Queryable queryable = getQueryable();
    if (queryable == null) {
        QueryableCollection collection = getQueryableCollection();
        if (!collection.getElementType().isEntityType()) {
            throw new QueryException("type discrimination cannot be applied to value collection [" + collection.getRole() + "]");
        }
        queryable = (Queryable) collection.getElementPersister();
    }
    handlePropertyBeingDereferenced(getDataType(), DISCRIMINATOR_PROPERTY_NAME);
    return new TypeDiscriminatorMetadataImpl(queryable.getTypeDiscriminatorMetadata(), aliasToUse);
}
Also used : QueryException(org.hibernate.QueryException) Queryable(org.hibernate.persister.entity.Queryable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 28 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class HqlSqlWalker method validateMapPropertyExpression.

@Override
protected void validateMapPropertyExpression(AST node) throws SemanticException {
    try {
        FromReferenceNode fromReferenceNode = (FromReferenceNode) node;
        QueryableCollection collectionPersister = fromReferenceNode.getFromElement().getQueryableCollection();
        if (!Map.class.isAssignableFrom(collectionPersister.getCollectionType().getReturnedClass())) {
            throw new SemanticException("node did not reference a map");
        }
    } catch (SemanticException se) {
        throw se;
    } catch (Throwable t) {
        throw new SemanticException("node did not reference a map");
    }
}
Also used : FromReferenceNode(org.hibernate.hql.internal.ast.tree.FromReferenceNode) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) Map(java.util.Map) HashMap(java.util.HashMap) SemanticException(antlr.SemanticException)

Example 29 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class HqlSqlWalker method createFromFilterElement.

@Override
protected AST createFromFilterElement(AST filterEntity, AST alias) throws SemanticException {
    FromElement fromElement = currentFromClause.addFromElement(filterEntity.getText(), alias);
    FromClause fromClause = fromElement.getFromClause();
    QueryableCollection persister = sessionFactoryHelper.getCollectionPersister(collectionFilterRole);
    // Get the names of the columns used to link between the collection
    // owner and the collection elements.
    String[] keyColumnNames = persister.getKeyColumnNames();
    String fkTableAlias = persister.isOneToMany() ? fromElement.getTableAlias() : fromClause.getAliasGenerator().createName(collectionFilterRole);
    JoinSequence join = sessionFactoryHelper.createJoinSequence();
    join.setRoot(persister, fkTableAlias);
    if (!persister.isOneToMany()) {
        join.addJoin((AssociationType) persister.getElementType(), fromElement.getTableAlias(), JoinType.INNER_JOIN, persister.getElementColumnNames(fkTableAlias));
    }
    join.addCondition(fkTableAlias, keyColumnNames, " = ?");
    fromElement.setJoinSequence(join);
    fromElement.setFilter(true);
    LOG.debug("createFromFilterElement() : processed filter FROM element.");
    return fromElement;
}
Also used : FromClause(org.hibernate.hql.internal.ast.tree.FromClause) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) EntityJoinFromElement(org.hibernate.hql.internal.ast.tree.EntityJoinFromElement) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 30 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class AbstractCompositionAttribute method getAttributes.

@Override
public Iterable<AttributeDefinition> getAttributes() {
    return new Iterable<AttributeDefinition>() {

        @Override
        public Iterator<AttributeDefinition> iterator() {
            return new Iterator<AttributeDefinition>() {

                private final int numberOfAttributes = getType().getSubtypes().length;

                private int currentSubAttributeNumber;

                private int currentColumnPosition = columnStartPosition;

                @Override
                public boolean hasNext() {
                    return currentSubAttributeNumber < numberOfAttributes;
                }

                @Override
                public AttributeDefinition next() {
                    final int subAttributeNumber = currentSubAttributeNumber;
                    currentSubAttributeNumber++;
                    final String name = getType().getPropertyNames()[subAttributeNumber];
                    final Type type = getType().getSubtypes()[subAttributeNumber];
                    int columnPosition = currentColumnPosition;
                    currentColumnPosition += type.getColumnSpan(sessionFactory());
                    if (type.isAssociationType()) {
                        // we build the association-key here because of the "goofiness" with 'currentColumnPosition'
                        final AssociationKey associationKey;
                        final AssociationType aType = (AssociationType) type;
                        final Joinable joinable = aType.getAssociatedJoinable(sessionFactory());
                        if (aType.isAnyType()) {
                            associationKey = new AssociationKey(JoinHelper.getLHSTableName(aType, attributeNumber(), (OuterJoinLoadable) locateOwningPersister()), JoinHelper.getLHSColumnNames(aType, attributeNumber(), columnPosition, (OuterJoinLoadable) locateOwningPersister(), sessionFactory()));
                        } else if (aType.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) locateOwningPersister();
                                lhsTableName = getLHSTableName(aType, attributeNumber(), entityPersister);
                                lhsColumnNames = getLHSColumnNames(aType, attributeNumber(), columnPosition, entityPersister, sessionFactory());
                            }
                            associationKey = new AssociationKey(lhsTableName, lhsColumnNames);
                        } else {
                            associationKey = new AssociationKey(joinable.getTableName(), getRHSColumnNames(aType, sessionFactory()));
                        }
                        final CompositeType cType = getType();
                        final boolean nullable = cType.getPropertyNullability() == null || cType.getPropertyNullability()[subAttributeNumber];
                        return new CompositeBasedAssociationAttribute(AbstractCompositionAttribute.this, sessionFactory(), attributeNumber(), name, (AssociationType) type, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(nullable).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation(), subAttributeNumber, associationKey);
                    } else if (type.isComponentType()) {
                        return new CompositionBasedCompositionAttribute(AbstractCompositionAttribute.this, sessionFactory(), attributeNumber(), name, (CompositeType) type, columnPosition, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(getType().getPropertyNullability()[subAttributeNumber]).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation());
                    } else {
                        final CompositeType cType = getType();
                        final boolean nullable = cType.getPropertyNullability() == null || cType.getPropertyNullability()[subAttributeNumber];
                        return new CompositeBasedBasicAttribute(AbstractCompositionAttribute.this, sessionFactory(), subAttributeNumber, name, type, new BaselineAttributeInformation.Builder().setInsertable(AbstractCompositionAttribute.this.isInsertable()).setUpdateable(AbstractCompositionAttribute.this.isUpdateable()).setNullable(nullable).setDirtyCheckable(true).setVersionable(AbstractCompositionAttribute.this.isVersionable()).setCascadeStyle(getType().getCascadeStyle(subAttributeNumber)).setFetchMode(getType().getFetchMode(subAttributeNumber)).createInformation());
                    }
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Remove operation not supported here");
                }
            };
        }
    };
}
Also used : AssociationKey(org.hibernate.persister.walking.spi.AssociationKey) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) AttributeDefinition(org.hibernate.persister.walking.spi.AttributeDefinition) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) Joinable(org.hibernate.persister.entity.Joinable) Iterator(java.util.Iterator) BaselineAttributeInformation(org.hibernate.tuple.BaselineAttributeInformation) CompositeType(org.hibernate.type.CompositeType)

Aggregations

QueryableCollection (org.hibernate.persister.collection.QueryableCollection)33 Type (org.hibernate.type.Type)10 QueryException (org.hibernate.QueryException)9 AssociationType (org.hibernate.type.AssociationType)7 JoinSequence (org.hibernate.engine.internal.JoinSequence)6 Joinable (org.hibernate.persister.entity.Joinable)6 JoinType (org.hibernate.sql.JoinType)6 CollectionType (org.hibernate.type.CollectionType)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)5 HashMap (java.util.HashMap)4 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)4 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)4 EntityType (org.hibernate.type.EntityType)4 SemanticException (antlr.SemanticException)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 Map (java.util.Map)3 Session (org.hibernate.Session)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3