Search in sources :

Example 56 with QueryException

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

the class EntityGraphQueryHint method getFromElements.

private List<FromElement> getFromElements(List attributeNodes, FromElement origin, FromClause fromClause, HqlSqlWalker walker, Map<String, FromElement> explicitFetches) {
    final List<FromElement> fromElements = new ArrayList<FromElement>();
    for (Object obj : attributeNodes) {
        final AttributeNode<?> attributeNode = (AttributeNode<?>) obj;
        final String attributeName = attributeNode.getAttributeName();
        final String className = origin.getClassName();
        // TODO: This is ignored by collection types and probably wrong for entity types.  Presumably it screws
        // with inheritance.
        final String role = className + "." + attributeName;
        final String classAlias = origin.getClassAlias();
        final String originTableAlias = origin.getTableAlias();
        final Type propertyType = origin.getPropertyType(attributeName, attributeName);
        try {
            FromElement fromElement = explicitFetches.get(role);
            boolean explicitFromElement = false;
            if (fromElement == null) {
                if (propertyType.isEntityType()) {
                    final EntityType entityType = (EntityType) propertyType;
                    final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
                    final String tableAlias = walker.getAliasGenerator().createName(entityType.getAssociatedEntityName());
                    final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
                    final JoinSequence joinSequence = walker.getSessionFactoryHelper().createJoinSequence(false, entityType, tableAlias, JoinType.LEFT_OUTER_JOIN, columns);
                    fromElement = fromElementFactory.createEntityJoin(entityType.getAssociatedEntityName(), tableAlias, joinSequence, true, walker.isInFrom(), entityType, role, null);
                } else if (propertyType.isCollectionType()) {
                    CollectionType collectionType = (CollectionType) propertyType;
                    final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
                    final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
                    final QueryableCollection queryableCollection = walker.getSessionFactoryHelper().requireQueryableCollection(collectionType.getRole());
                    fromElement = fromElementFactory.createCollection(queryableCollection, collectionType.getRole(), JoinType.LEFT_OUTER_JOIN, true, false);
                }
            } else {
                explicitFromElement = true;
                fromElement.setInProjectionList(true);
                fromElement.setFetch(true);
            }
            if (fromElement != null) {
                if (!explicitFromElement) {
                    fromElements.add(fromElement);
                }
                // recurse into subgraphs
                for (Subgraph<?> subgraph : attributeNode.getSubgraphs().values()) {
                    fromElements.addAll(getFromElements(subgraph.getAttributeNodes(), fromElement, fromClause, walker, explicitFetches));
                }
            }
        } catch (Exception e) {
            throw new QueryException("Could not apply the EntityGraph to the Query!", e);
        }
    }
    return fromElements;
}
Also used : AttributeNode(javax.persistence.AttributeNode) ArrayList(java.util.ArrayList) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) QueryException(org.hibernate.QueryException) EntityType(org.hibernate.type.EntityType) JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) ImpliedFromElement(org.hibernate.hql.internal.ast.tree.ImpliedFromElement) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) CollectionType(org.hibernate.type.CollectionType) FromElementFactory(org.hibernate.hql.internal.ast.tree.FromElementFactory) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 57 with QueryException

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

the class CriteriaQueryTranslator method createAssociationPathCriteriaMap.

private void createAssociationPathCriteriaMap() {
    final Iterator<CriteriaImpl.Subcriteria> iter = rootCriteria.iterateSubcriteria();
    while (iter.hasNext()) {
        CriteriaImpl.Subcriteria crit = iter.next();
        String wholeAssociationPath = getWholeAssociationPath(crit);
        Object old = associationPathCriteriaMap.put(wholeAssociationPath, crit);
        if (old != null) {
            throw new QueryException("duplicate association path: " + wholeAssociationPath);
        }
        JoinType joinType = crit.getJoinType();
        old = associationPathJoinTypesMap.put(wholeAssociationPath, joinType);
        if (old != null) {
            // TODO : not so sure this is needed...
            throw new QueryException("duplicate association path: " + wholeAssociationPath);
        }
        if (crit.getWithClause() != null) {
            this.withClauseMap.put(wholeAssociationPath, crit.getWithClause());
        }
    }
}
Also used : QueryException(org.hibernate.QueryException) CriteriaImpl(org.hibernate.internal.CriteriaImpl) JoinType(org.hibernate.sql.JoinType)

Example 58 with QueryException

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

the class CriteriaQueryTranslator method createAliasCriteriaMap.

private void createAliasCriteriaMap() {
    aliasCriteriaMap.put(rootCriteria.getAlias(), rootCriteria);
    Iterator<CriteriaImpl.Subcriteria> iter = rootCriteria.iterateSubcriteria();
    while (iter.hasNext()) {
        Criteria subcriteria = iter.next();
        if (subcriteria.getAlias() != null) {
            Object old = aliasCriteriaMap.put(subcriteria.getAlias(), subcriteria);
            if (old != null) {
                throw new QueryException("duplicate alias: " + subcriteria.getAlias());
            }
        }
    }
}
Also used : QueryException(org.hibernate.QueryException) Criteria(org.hibernate.Criteria)

Example 59 with QueryException

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

the class CriteriaQueryTranslator method getTypeUsingProjection.

@Override
public Type getTypeUsingProjection(Criteria subcriteria, String propertyName) throws HibernateException {
    //first look for a reference to a projection alias
    final Projection projection = rootCriteria.getProjection();
    Type[] projectionTypes = projection == null ? null : projection.getTypes(propertyName, subcriteria, this);
    if (projectionTypes == null) {
        try {
            //look for a property
            return getType(subcriteria, propertyName);
        } catch (HibernateException he) {
            //not found in inner query , try the outer query
            if (outerQueryTranslator != null) {
                return outerQueryTranslator.getType(subcriteria, propertyName);
            } else {
                throw he;
            }
        }
    } else {
        if (projectionTypes.length != 1) {
            //should never happen, i think
            throw new QueryException("not a single-length projection: " + propertyName);
        }
        return projectionTypes[0];
    }
}
Also used : StringRepresentableType(org.hibernate.type.StringRepresentableType) CollectionType(org.hibernate.type.CollectionType) JoinType(org.hibernate.sql.JoinType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) HibernateException(org.hibernate.HibernateException) EnhancedProjection(org.hibernate.criterion.EnhancedProjection) Projection(org.hibernate.criterion.Projection)

Example 60 with QueryException

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

the class BulkManipulationTest method testUpdateOnImplicitJoinFails.

@Test
public void testUpdateOnImplicitJoinFails() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Human human = new Human();
    human.setName(new Name("Steve", 'E', null));
    Human mother = new Human();
    mother.setName(new Name("Jane", 'E', null));
    human.setMother(mother);
    s.save(human);
    s.save(mother);
    s.flush();
    t.commit();
    t = s.beginTransaction();
    try {
        s.createQuery("update Human set mother.name.initial = :initial").setString("initial", "F").executeUpdate();
        fail("update allowed across implicit join");
    } catch (IllegalArgumentException e) {
        assertTyping(QueryException.class, e.getCause());
    } catch (QueryException e) {
    }
    s.createQuery("delete Human where mother is not null").executeUpdate();
    s.createQuery("delete Human").executeUpdate();
    t.commit();
    s.close();
}
Also used : QueryException(org.hibernate.QueryException) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

QueryException (org.hibernate.QueryException)77 Type (org.hibernate.type.Type)20 Test (org.junit.Test)19 Session (org.hibernate.Session)18 Transaction (org.hibernate.Transaction)12 JoinType (org.hibernate.sql.JoinType)12 Queryable (org.hibernate.persister.entity.Queryable)10 CollectionType (org.hibernate.type.CollectionType)10 MappingException (org.hibernate.MappingException)9 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)9 AssociationType (org.hibernate.type.AssociationType)8 HashMap (java.util.HashMap)6 HibernateException (org.hibernate.HibernateException)6 JoinSequence (org.hibernate.engine.internal.JoinSequence)6 EntityType (org.hibernate.type.EntityType)6 AST (antlr.collections.AST)5 Map (java.util.Map)5 SemanticException (antlr.SemanticException)4 ArrayList (java.util.ArrayList)4 FromElement (org.hibernate.hql.internal.ast.tree.FromElement)4