Search in sources :

Example 6 with Queryable

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

the class FromElementFactory method createFromElementInSubselect.

private FromElement createFromElementInSubselect(String path, String pathAlias, FromElement parentFromElement, String classAlias) throws SemanticException {
    LOG.debugf("createFromElementInSubselect() : path = %s", path);
    // Create an DotNode AST for the path and resolve it.
    FromElement fromElement = evaluateFromElementPath(path, classAlias);
    EntityPersister entityPersister = fromElement.getEntityPersister();
    // If the first identifier in the path refers to the class alias (not the class name), then this
    // is a correlated subselect.  If it's a correlated sub-select, use the existing table alias.  Otherwise
    // generate a new one.
    String tableAlias = null;
    boolean correlatedSubselect = pathAlias.equals(parentFromElement.getClassAlias());
    if (correlatedSubselect) {
        tableAlias = fromElement.getTableAlias();
    } else {
        tableAlias = null;
    }
    // If the from element isn't in the same clause, create a new from element.
    if (fromElement.getFromClause() != fromClause) {
        LOG.debug("createFromElementInSubselect() : creating a new FROM element...");
        fromElement = createFromElement(entityPersister);
        initializeAndAddFromElement(fromElement, path, classAlias, entityPersister, (EntityType) ((Queryable) entityPersister).getType(), tableAlias);
    }
    LOG.debugf("createFromElementInSubselect() : %s -> %s", path, fromElement);
    return fromElement;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Queryable(org.hibernate.persister.entity.Queryable)

Example 7 with Queryable

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

the class IdentNode method resolveAsNakedPropertyRef.

private DereferenceType resolveAsNakedPropertyRef() {
    FromElement fromElement = locateSingleFromElement();
    if (fromElement == null) {
        return DereferenceType.UNKNOWN;
    }
    Queryable persister = fromElement.getQueryable();
    if (persister == null) {
        return DereferenceType.UNKNOWN;
    }
    Type propertyType = getNakedPropertyType(fromElement);
    if (propertyType == null) {
        // assume this ident's text does *not* refer to a property on the given persister
        return DereferenceType.UNKNOWN;
    }
    if ((propertyType.isComponentType() || propertyType.isAssociationType())) {
        return DereferenceType.COMPONENT_REF;
    }
    setFromElement(fromElement);
    String property = getText();
    String[] columns = getWalker().isSelectStatement() ? persister.toColumns(fromElement.getTableAlias(), property) : persister.toColumns(property);
    String text = StringHelper.join(", ", columns);
    setText(columns.length == 1 ? text : "(" + text + ")");
    setType(SqlTokenTypes.SQL_TOKEN);
    // these pieces are needed for usage in select clause
    super.setDataType(propertyType);
    nakedPropertyRef = true;
    return DereferenceType.PROPERTY_REF;
}
Also used : JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) Type(org.hibernate.type.Type) Queryable(org.hibernate.persister.entity.Queryable)

Example 8 with Queryable

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

the class QueryTranslatorImpl method renderSQL.

private void renderSQL() throws QueryException, MappingException {
    final int rtsize;
    if (returnedTypes.size() == 0 && scalarTypes.size() == 0) {
        //ie no select clause in HQL
        returnedTypes = fromTypes;
        rtsize = returnedTypes.size();
    } else {
        rtsize = returnedTypes.size();
        Iterator iter = entitiesToFetch.iterator();
        while (iter.hasNext()) {
            returnedTypes.add(iter.next());
        }
    }
    int size = returnedTypes.size();
    persisters = new Queryable[size];
    names = new String[size];
    owners = new int[size];
    ownerAssociationTypes = new EntityType[size];
    suffixes = new String[size];
    includeInSelect = new boolean[size];
    for (int i = 0; i < size; i++) {
        String name = (String) returnedTypes.get(i);
        //if ( !isName(name) ) throw new QueryException("unknown type: " + name);
        persisters[i] = getEntityPersisterForName(name);
        // TODO: cannot use generateSuffixes() - it handles the initial suffix differently.
        suffixes[i] = (size == 1) ? "" : Integer.toString(i) + '_';
        names[i] = name;
        includeInSelect[i] = !entitiesToFetch.contains(name);
        if (includeInSelect[i]) {
            selectLength++;
        }
        if (name.equals(collectionOwnerName)) {
            collectionOwnerColumn = i;
        }
        String oneToOneOwner = (String) oneToOneOwnerNames.get(name);
        owners[i] = (oneToOneOwner == null) ? -1 : returnedTypes.indexOf(oneToOneOwner);
        ownerAssociationTypes[i] = (EntityType) uniqueKeyOwnerReferences.get(name);
    }
    if (ArrayHelper.isAllNegative(owners)) {
        owners = null;
    }
    //Must be done here because of side-effect! yuck...
    String scalarSelect = renderScalarSelect();
    int scalarSize = scalarTypes.size();
    hasScalars = scalarTypes.size() != rtsize;
    returnTypes = new Type[scalarSize];
    for (int i = 0; i < scalarSize; i++) {
        returnTypes[i] = (Type) scalarTypes.get(i);
    }
    QuerySelect sql = new QuerySelect(getFactory().getDialect());
    sql.setDistinct(distinct);
    if (!shallowQuery) {
        renderIdentifierSelect(sql);
        renderPropertiesSelect(sql);
    }
    if (collectionPersister != null) {
        sql.addSelectFragmentString(collectionPersister.selectFragment(fetchName, "__"));
    }
    if (hasScalars || shallowQuery) {
        sql.addSelectFragmentString(scalarSelect);
    }
    //TODO: for some dialects it would be appropriate to add the renderOrderByPropertiesSelect() to other select strings
    mergeJoins(sql.getJoinFragment());
    sql.setWhereTokens(whereTokens.iterator());
    sql.setGroupByTokens(groupByTokens.iterator());
    sql.setHavingTokens(havingTokens.iterator());
    sql.setOrderByTokens(orderByTokens.iterator());
    if (collectionPersister != null && collectionPersister.hasOrdering()) {
        sql.addOrderBy(collectionPersister.getSQLOrderByString(fetchName));
    }
    scalarColumnNames = NameGenerator.generateColumnNames(returnTypes, getFactory());
    // initialize the Set of queried identifier spaces (ie. tables)
    Iterator iter = collections.values().iterator();
    while (iter.hasNext()) {
        CollectionPersister p = getCollectionPersister((String) iter.next());
        addQuerySpaces(p.getCollectionSpaces());
    }
    iter = typeMap.keySet().iterator();
    while (iter.hasNext()) {
        Queryable p = getEntityPersisterForName((String) iter.next());
        addQuerySpaces(p.getQuerySpaces());
    }
    sqlString = sql.toQueryString();
    if (holderClass != null) {
        holderConstructor = ReflectHelper.getConstructor(holderClass, returnTypes);
    }
    if (hasScalars) {
        actualReturnTypes = returnTypes;
    } else {
        actualReturnTypes = new Type[selectLength];
        int j = 0;
        for (int i = 0; i < persisters.length; i++) {
            if (includeInSelect[i]) {
                actualReturnTypes[j++] = getFactory().getTypeResolver().getTypeFactory().manyToOne(persisters[i].getEntityName(), shallowQuery);
            }
        }
    }
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Queryable(org.hibernate.persister.entity.Queryable) Iterator(java.util.Iterator) QuerySelect(org.hibernate.sql.QuerySelect)

Example 9 with Queryable

use of org.hibernate.persister.entity.Queryable 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 10 with Queryable

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

the class LocalTemporaryTableBulkIdStrategy method buildDeleteHandler.

@Override
public DeleteHandler buildDeleteHandler(SessionFactoryImplementor factory, HqlSqlWalker walker) {
    final DeleteStatement updateStatement = (DeleteStatement) walker.getAST();
    final FromElement fromElement = updateStatement.getFromClause().getFromElement();
    final Queryable targetedPersister = fromElement.getQueryable();
    final IdTableInfoImpl tableInfo = getIdTableInfo(targetedPersister);
    return new TableBasedDeleteHandlerImpl(factory, walker, tableInfo) {

        @Override
        protected void prepareForUse(Queryable persister, SharedSessionContractImplementor session) {
            Helper.INSTANCE.createTempTable(tableInfo, ddlTransactionHandling, session);
        }

        @Override
        protected void releaseFromUse(Queryable persister, SharedSessionContractImplementor session) {
            Helper.INSTANCE.releaseTempTable(tableInfo, afterUseAction, ddlTransactionHandling, session);
        }
    };
}
Also used : TableBasedDeleteHandlerImpl(org.hibernate.hql.spi.id.TableBasedDeleteHandlerImpl) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) Queryable(org.hibernate.persister.entity.Queryable) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) DeleteStatement(org.hibernate.hql.internal.ast.tree.DeleteStatement)

Aggregations

Queryable (org.hibernate.persister.entity.Queryable)34 QueryException (org.hibernate.QueryException)10 FromElement (org.hibernate.hql.internal.ast.tree.FromElement)8 Type (org.hibernate.type.Type)7 CollectionType (org.hibernate.type.CollectionType)5 EntityType (org.hibernate.type.EntityType)5 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)4 UpdateStatement (org.hibernate.hql.internal.ast.tree.UpdateStatement)4 DeleteStatement (org.hibernate.hql.internal.ast.tree.DeleteStatement)3 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 JoinType (org.hibernate.sql.JoinType)3 AST (antlr.collections.AST)2 InsertStatement (org.hibernate.hql.internal.ast.tree.InsertStatement)2 ParameterNode (org.hibernate.hql.internal.ast.tree.ParameterNode)2 TableBasedDeleteHandlerImpl (org.hibernate.hql.spi.id.TableBasedDeleteHandlerImpl)2 TableBasedUpdateHandlerImpl (org.hibernate.hql.spi.id.TableBasedUpdateHandlerImpl)2 CollectionFilterKeyParameterSpecification (org.hibernate.param.CollectionFilterKeyParameterSpecification)2 NamedParameterSpecification (org.hibernate.param.NamedParameterSpecification)2 ParameterSpecification (org.hibernate.param.ParameterSpecification)2