Search in sources :

Example 16 with EntityType

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

the class QueryTranslatorImpl method addFromAssociation.

/**
	 * Used for collection filters
	 */
private void addFromAssociation(final String elementName, final String collectionRole) throws QueryException {
    //q.addCollection(collectionName, collectionRole);
    QueryableCollection persister = getCollectionPersister(collectionRole);
    Type collectionElementType = persister.getElementType();
    if (!collectionElementType.isEntityType()) {
        throw new QueryException("collection of values in filter: " + elementName);
    }
    String[] keyColumnNames = persister.getKeyColumnNames();
    //if (keyColumnNames.length!=1) throw new QueryException("composite-key collection in filter: " + collectionRole);
    String collectionName;
    JoinSequence join = new JoinSequence(getFactory());
    collectionName = persister.isOneToMany() ? elementName : createNameForCollection(collectionRole);
    join.setRoot(persister, collectionName);
    if (!persister.isOneToMany()) {
        //many-to-many
        addCollection(collectionName, collectionRole);
        try {
            join.addJoin((AssociationType) persister.getElementType(), elementName, JoinType.INNER_JOIN, persister.getElementColumnNames(collectionName));
        } catch (MappingException me) {
            throw new QueryException(me);
        }
    }
    join.addCondition(collectionName, keyColumnNames, " = ?");
    //if ( persister.hasWhere() ) join.addCondition( persister.getSQLWhereString(collectionName) );
    EntityType elemType = (EntityType) collectionElementType;
    addFrom(elementName, elemType.getAssociatedEntityName(), join);
}
Also used : EntityType(org.hibernate.type.EntityType) JoinType(org.hibernate.sql.JoinType) EntityType(org.hibernate.type.EntityType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) JoinSequence(org.hibernate.engine.internal.JoinSequence) MappingException(org.hibernate.MappingException)

Example 17 with EntityType

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

the class ForeignGenerator method generate.

@Override
public Serializable generate(SharedSessionContractImplementor sessionImplementor, Object object) {
    // needs to be a Session for the #save and #contains calls below...
    final Session session = (Session) sessionImplementor;
    final EntityPersister persister = sessionImplementor.getFactory().getMetamodel().entityPersister(entityName);
    Object associatedObject = persister.getPropertyValue(object, propertyName);
    if (associatedObject == null) {
        throw new IdentifierGenerationException("attempted to assign id from null one-to-one property [" + getRole() + "]");
    }
    final EntityType foreignValueSourceType;
    final Type propertyType = persister.getPropertyType(propertyName);
    if (propertyType.isEntityType()) {
        // the normal case
        foreignValueSourceType = (EntityType) propertyType;
    } else {
        // try identifier mapper
        foreignValueSourceType = (EntityType) persister.getPropertyType(PropertyPath.IDENTIFIER_MAPPER_PROPERTY + "." + propertyName);
    }
    Serializable id;
    try {
        id = ForeignKeys.getEntityIdentifierIfNotUnsaved(foreignValueSourceType.getAssociatedEntityName(), associatedObject, sessionImplementor);
    } catch (TransientObjectException toe) {
        id = session.save(foreignValueSourceType.getAssociatedEntityName(), associatedObject);
    }
    if (session.contains(entityName, object)) {
        //abort the save (the object is already saved by a circular cascade)
        return IdentifierGeneratorHelper.SHORT_CIRCUIT_INDICATOR;
    //throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association");
    }
    return id;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityType(org.hibernate.type.EntityType) TransientObjectException(org.hibernate.TransientObjectException) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) Serializable(java.io.Serializable) Session(org.hibernate.Session)

Example 18 with EntityType

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

the class WhereParser method getElementName.

private String getElementName(PathExpressionParser.CollectionElement element, QueryTranslatorImpl q) throws QueryException {
    String name;
    if (element.isOneToMany) {
        name = element.alias;
    } else {
        Type type = element.elementType;
        if (type.isEntityType()) {
            //ie. a many-to-many
            String entityName = ((EntityType) type).getAssociatedEntityName();
            name = pathExpressionParser.continueFromManyToMany(entityName, element.elementColumns, q);
        } else {
            throw new QueryException("illegally dereferenced collection element");
        }
    }
    return name;
}
Also used : EntityType(org.hibernate.type.EntityType) LiteralType(org.hibernate.type.LiteralType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException)

Example 19 with EntityType

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

the class DotNode method resolve.

public void resolve(boolean generateJoin, boolean implicitJoin, String classAlias, AST parent, AST parentPredicate) throws SemanticException {
    // If this dot has already been resolved, stop now.
    if (isResolved()) {
        return;
    }
    // Prepare the left hand side and get the data type.
    Type propertyType = prepareLhs();
    if (parent == null && AbstractEntityPersister.ENTITY_CLASS.equals(propertyName)) {
        DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfClassEntityTypeSelector(getLhs().getPath());
    }
    // this might be a Java constant.
    if (propertyType == null) {
        if (parent == null) {
            getWalker().getLiteralProcessor().lookupConstant(this);
        }
        // stop now... there was a problem resolving the node anyway.
        return;
    }
    if (propertyType.isComponentType()) {
        // The property is a component...
        checkLhsIsNotCollection();
        dereferenceComponent(parent);
        initText();
    } else if (propertyType.isEntityType()) {
        // The property is another class..
        checkLhsIsNotCollection();
        dereferenceEntity((EntityType) propertyType, implicitJoin, classAlias, generateJoin, parent, parentPredicate);
        initText();
    } else if (propertyType.isCollectionType()) {
        // The property is a collection...
        checkLhsIsNotCollection();
        dereferenceCollection((CollectionType) propertyType, implicitJoin, false, classAlias, parent);
    } else {
        // Otherwise, this is a primitive type.
        if (!CollectionProperties.isAnyCollectionProperty(propertyName)) {
            checkLhsIsNotCollection();
        }
        dereferenceType = DereferenceType.PRIMITIVE;
        initText();
    }
    setResolved();
}
Also used : 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)

Example 20 with EntityType

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

the class MapEntryNode method determineValueSelectExpressions.

private void determineValueSelectExpressions(QueryableCollection collectionPersister, List selections) {
    AliasGenerator aliasGenerator = new LocalAliasGenerator(1);
    appendSelectExpressions(collectionPersister.getElementColumnNames(), selections, aliasGenerator);
    Type valueType = collectionPersister.getElementType();
    if (valueType.isAssociationType()) {
        EntityType valueEntityType = (EntityType) valueType;
        Queryable valueEntityPersister = (Queryable) sfi().getEntityPersister(valueEntityType.getAssociatedEntityName(sfi()));
        SelectFragment fragment = valueEntityPersister.propertySelectFragmentFragment(elementTableAlias(), null, false);
        appendSelectExpressions(fragment, selections, aliasGenerator);
    }
}
Also used : EntityType(org.hibernate.type.EntityType) SelectFragment(org.hibernate.sql.SelectFragment) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) AliasGenerator(org.hibernate.sql.AliasGenerator) Queryable(org.hibernate.persister.entity.Queryable)

Aggregations

EntityType (org.hibernate.type.EntityType)28 Type (org.hibernate.type.Type)18 EntityPersister (org.hibernate.persister.entity.EntityPersister)9 JoinSequence (org.hibernate.engine.internal.JoinSequence)6 JoinType (org.hibernate.sql.JoinType)6 CompositeType (org.hibernate.type.CompositeType)6 AssociationType (org.hibernate.type.AssociationType)5 CollectionType (org.hibernate.type.CollectionType)5 QueryException (org.hibernate.QueryException)4 Queryable (org.hibernate.persister.entity.Queryable)4 MappingException (org.hibernate.MappingException)3 EntityKey (org.hibernate.engine.spi.EntityKey)3 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)3 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)3 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 HibernateException (org.hibernate.HibernateException)2 Session (org.hibernate.Session)2