Search in sources :

Example 6 with CollectionType

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

the class DotNode method resolveIndex.

public void resolveIndex(AST parent) throws SemanticException {
    if (isResolved()) {
        return;
    }
    // Prepare the left hand side and get the data type.
    Type propertyType = prepareLhs();
    dereferenceCollection((CollectionType) propertyType, true, true, null, parent);
}
Also used : JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type)

Example 7 with CollectionType

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

the class PathExpressionParser method token.

public void token(String token, QueryTranslatorImpl q) throws QueryException {
    if (token != null) {
        path.append(token);
    }
    String alias = q.getPathAlias(path.toString());
    if (alias != null) {
        //reset the dotcount (but not the path)
        reset(q);
        //afterQuery reset!
        currentName = alias;
        currentPropertyMapping = q.getPropertyMapping(currentName);
        if (!ignoreInitialJoin) {
            JoinSequence ojf = q.getPathJoin(path.toString());
            try {
                //afterQuery reset!
                joinSequence.addCondition(ojf.toJoinFragment(q.getEnabledFilters(), true).toWhereFragmentString());
            } catch (MappingException me) {
                throw new QueryException(me);
            }
        // we don't need to worry about any condition in the ON clause
        // here (toFromFragmentString), since anything in the ON condition
        // is already applied to the whole query
        }
    } else if (".".equals(token)) {
        dotcount++;
    } else {
        if (dotcount == 0) {
            if (!continuation) {
                if (!q.isName(token)) {
                    throw new QueryException("undefined alias: " + token);
                }
                currentName = token;
                currentPropertyMapping = q.getPropertyMapping(currentName);
            }
        } else if (dotcount == 1) {
            if (currentName != null) {
                currentProperty = token;
            } else if (collectionName != null) {
                //processCollectionProperty(token, q.getCollectionPersister(collectionRole), collectionName);
                continuation = false;
            } else {
                throw new QueryException("unexpected");
            }
        } else {
            // dotcount>=2
            // Do the corresponding RHS
            Type propertyType = getPropertyType();
            if (propertyType == null) {
                throw new QueryException("unresolved property: " + path);
            }
            if (propertyType.isComponentType()) {
                dereferenceComponent(token);
            } else if (propertyType.isEntityType()) {
                if (!isCollectionValued()) {
                    dereferenceEntity(token, (EntityType) propertyType, q);
                }
            } else if (propertyType.isCollectionType()) {
                dereferenceCollection(token, ((CollectionType) propertyType).getRole(), q);
            } else {
                if (token != null) {
                    throw new QueryException("dereferenced: " + path);
                }
            }
        }
    }
}
Also used : EntityType(org.hibernate.type.EntityType) QueryException(org.hibernate.QueryException) 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) JoinSequence(org.hibernate.engine.internal.JoinSequence) MappingException(org.hibernate.MappingException)

Example 8 with CollectionType

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

the class PathExpressionParser method end.

public void end(QueryTranslatorImpl q) throws QueryException {
    ignoreInitialJoin = false;
    Type propertyType = getPropertyType();
    if (propertyType != null && propertyType.isCollectionType()) {
        collectionRole = ((CollectionType) propertyType).getRole();
        collectionName = q.createNameForCollection(collectionRole);
        prepareForIndex(q);
    } else {
        columns = currentColumns();
        setType();
    }
    //important!!
    continuation = false;
}
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)

Example 9 with CollectionType

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

the class AbstractInlineIdsDeleteHandlerImpl method execute.

@Override
public int execute(SharedSessionContractImplementor session, QueryParameters queryParameters) {
    IdsClauseBuilder values = prepareInlineStatement(session, queryParameters);
    if (!values.getIds().isEmpty()) {
        final String idSubselect = values.toStatement();
        for (Type type : getTargetedQueryable().getPropertyTypes()) {
            if (type.isCollectionType()) {
                CollectionType cType = (CollectionType) type;
                AbstractCollectionPersister cPersister = (AbstractCollectionPersister) factory().getMetamodel().collectionPersister(cType.getRole());
                if (cPersister.isManyToMany()) {
                    deletes.add(generateDelete(cPersister.getTableName(), cPersister.getKeyColumnNames(), idSubselect, "bulk delete - m2m join table cleanup").toStatementString());
                }
            }
        }
        String[] tableNames = getTargetedQueryable().getConstraintOrderedTableNameClosure();
        String[][] columnNames = getTargetedQueryable().getContraintOrderedTableKeyColumnClosure();
        for (int i = 0; i < tableNames.length; i++) {
            // TODO : an optimization here would be to consider cascade deletes and not gen those delete statements;
            //      the difficulty is the ordering of the tables here vs the cascade attributes on the persisters ->
            //          the table info gotten here should really be self-contained (i.e., a class representation
            //          defining all the needed attributes), then we could then get an array of those
            deletes.add(generateDelete(tableNames[i], columnNames[i], idSubselect, "bulk delete").toStatementString());
        }
        // Start performing the deletes
        for (String delete : deletes) {
            if (delete == null) {
                continue;
            }
            try {
                try (PreparedStatement ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(delete, false)) {
                    session.getJdbcCoordinator().getResultSetReturn().executeUpdate(ps);
                }
            } catch (SQLException e) {
                throw convert(e, "error performing bulk delete", delete);
            }
        }
    }
    return values.getIds().size();
}
Also used : CollectionType(org.hibernate.type.CollectionType) Type(org.hibernate.type.Type) SQLException(java.sql.SQLException) CollectionType(org.hibernate.type.CollectionType) PreparedStatement(java.sql.PreparedStatement) AbstractCollectionPersister(org.hibernate.persister.collection.AbstractCollectionPersister)

Example 10 with CollectionType

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

the class QuerySpaceHelper method makeCollectionQuerySpace.

public ExpandingCollectionQuerySpace makeCollectionQuerySpace(ExpandingQuerySpace lhsQuerySpace, AssociationAttributeDefinition attributeDefinition, String querySpaceUid, FetchStrategy fetchStrategy) {
    final CollectionType fetchedType = (CollectionType) attributeDefinition.getType();
    final CollectionPersister fetchedPersister = attributeDefinition.toCollectionDefinition().getCollectionPersister();
    if (fetchedPersister == null) {
        throw new WalkingException(String.format("Unable to locate CollectionPersister [%s] for fetch [%s]", fetchedType.getRole(), attributeDefinition.getName()));
    }
    final boolean required = lhsQuerySpace.canJoinsBeRequired() && !attributeDefinition.isNullable();
    final ExpandingCollectionQuerySpace rhs = lhsQuerySpace.getExpandingQuerySpaces().makeCollectionQuerySpace(querySpaceUid, fetchedPersister, required);
    if (shouldIncludeJoin(fetchStrategy)) {
        final JoinDefinedByMetadata join = JoinHelper.INSTANCE.createCollectionJoin(lhsQuerySpace, attributeDefinition.getName(), rhs, required, (CollectionType) attributeDefinition.getType(), fetchedPersister.getFactory());
        lhsQuerySpace.addJoin(join);
    }
    return rhs;
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionType(org.hibernate.type.CollectionType) JoinDefinedByMetadata(org.hibernate.loader.plan.spi.JoinDefinedByMetadata) WalkingException(org.hibernate.persister.walking.spi.WalkingException) ExpandingCollectionQuerySpace(org.hibernate.loader.plan.build.spi.ExpandingCollectionQuerySpace)

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