Search in sources :

Example 21 with EntityType

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

the class FromElementFactory method createElementJoin.

FromElement createElementJoin(QueryableCollection queryableCollection) throws SemanticException {
    FromElement elem;
    //TODO: always true for now, but not if we later decide to support elements() in the from clause
    implied = true;
    inElementsFunction = true;
    Type elementType = queryableCollection.getElementType();
    if (!elementType.isEntityType()) {
        throw new IllegalArgumentException("Cannot create element join for a collection of non-entities!");
    }
    this.queryableCollection = queryableCollection;
    SessionFactoryHelper sfh = fromClause.getSessionFactoryHelper();
    FromElement destination = null;
    String tableAlias = null;
    EntityPersister entityPersister = queryableCollection.getElementPersister();
    tableAlias = fromClause.getAliasGenerator().createName(entityPersister.getEntityName());
    String associatedEntityName = entityPersister.getEntityName();
    EntityPersister targetEntityPersister = sfh.requireClassPersister(associatedEntityName);
    // Create the FROM element for the target (the elements of the collection).
    destination = createAndAddFromElement(associatedEntityName, classAlias, targetEntityPersister, (EntityType) queryableCollection.getElementType(), tableAlias);
    // If the join is implied, then don't include sub-classes on the element.
    if (implied) {
        destination.setIncludeSubclasses(false);
    }
    fromClause.addCollectionJoinFromElementByPath(path, destination);
    //		origin.addDestination(destination);
    // Add the query spaces.
    fromClause.getWalker().addQuerySpaces(entityPersister.getQuerySpaces());
    CollectionType type = queryableCollection.getCollectionType();
    String role = type.getRole();
    String roleAlias = origin.getTableAlias();
    String[] targetColumns = sfh.getCollectionElementColumns(role, roleAlias);
    AssociationType elementAssociationType = sfh.getElementAssociationType(type);
    // Create the join element under the from element.
    JoinType joinType = JoinType.INNER_JOIN;
    JoinSequence joinSequence = sfh.createJoinSequence(implied, elementAssociationType, tableAlias, joinType, targetColumns);
    elem = initializeJoin(path, destination, joinSequence, targetColumns, origin, false);
    // The associated entity is implied, but it must be included in the FROM.
    elem.setUseFromFragment(true);
    // The collection alias is the role.
    elem.setCollectionTableAlias(roleAlias);
    return elem;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityType(org.hibernate.type.EntityType) JoinType(org.hibernate.sql.JoinType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) CollectionType(org.hibernate.type.CollectionType) SessionFactoryHelper(org.hibernate.hql.internal.ast.util.SessionFactoryHelper) JoinType(org.hibernate.sql.JoinType) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 22 with EntityType

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

the class FromElementFactory method createEntityAssociation.

private FromElement createEntityAssociation(String role, String roleAlias, JoinType joinType) throws SemanticException {
    FromElement elem;
    Queryable entityPersister = (Queryable) queryableCollection.getElementPersister();
    String associatedEntityName = entityPersister.getEntityName();
    // Get the class name of the associated entity.
    if (queryableCollection.isOneToMany()) {
        LOG.debugf("createEntityAssociation() : One to many - path = %s role = %s associatedEntityName = %s", path, role, associatedEntityName);
        JoinSequence joinSequence = createJoinSequence(roleAlias, joinType);
        elem = createJoin(associatedEntityName, roleAlias, joinSequence, (EntityType) queryableCollection.getElementType(), false);
    } else {
        LOG.debugf("createManyToMany() : path = %s role = %s associatedEntityName = %s", path, role, associatedEntityName);
        elem = createManyToMany(role, associatedEntityName, roleAlias, entityPersister, (EntityType) queryableCollection.getElementType(), joinType);
        fromClause.getWalker().addQuerySpaces(queryableCollection.getCollectionSpaces());
    }
    elem.setCollectionTableAlias(roleAlias);
    return elem;
}
Also used : EntityType(org.hibernate.type.EntityType) Queryable(org.hibernate.persister.entity.Queryable) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 23 with EntityType

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

the class CustomLoader method determineAppropriateOwnerPersister.

private Queryable determineAppropriateOwnerPersister(NonScalarReturn ownerDescriptor) {
    String entityName = null;
    if (ownerDescriptor instanceof RootReturn) {
        entityName = ((RootReturn) ownerDescriptor).getEntityName();
    } else if (ownerDescriptor instanceof CollectionReturn) {
        CollectionReturn collRtn = (CollectionReturn) ownerDescriptor;
        String role = collRtn.getOwnerEntityName() + "." + collRtn.getOwnerProperty();
        CollectionPersister persister = getFactory().getMetamodel().collectionPersister(role);
        EntityType ownerType = (EntityType) persister.getElementType();
        entityName = ownerType.getAssociatedEntityName(getFactory());
    } else if (ownerDescriptor instanceof FetchReturn) {
        FetchReturn fetchRtn = (FetchReturn) ownerDescriptor;
        Queryable persister = determineAppropriateOwnerPersister(fetchRtn.getOwner());
        Type ownerType = persister.getPropertyType(fetchRtn.getOwnerProperty());
        if (ownerType.isEntityType()) {
            entityName = ((EntityType) ownerType).getAssociatedEntityName(getFactory());
        } else if (ownerType.isCollectionType()) {
            Type ownerCollectionElementType = ((CollectionType) ownerType).getElementType(getFactory());
            if (ownerCollectionElementType.isEntityType()) {
                entityName = ((EntityType) ownerCollectionElementType).getAssociatedEntityName(getFactory());
            }
        }
    }
    if (entityName == null) {
        throw new HibernateException("Could not determine fetch owner : " + ownerDescriptor);
    }
    return (Queryable) getFactory().getMetamodel().entityPersister(entityName);
}
Also used : EntityType(org.hibernate.type.EntityType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) HibernateException(org.hibernate.HibernateException) Queryable(org.hibernate.persister.entity.Queryable)

Example 24 with EntityType

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

the class EntityReferenceInitializerImpl method loadFromResultSet.

private void loadFromResultSet(ResultSet resultSet, ResultSetProcessingContext context, Object entityInstance, String concreteEntityTypeName, EntityKey entityKey, LockMode lockModeToAcquire) {
    final Serializable id = entityKey.getIdentifier();
    // Get the persister for the _subclass_
    final Loadable concreteEntityPersister = (Loadable) context.getSession().getFactory().getMetamodel().entityPersister(concreteEntityTypeName);
    if (log.isTraceEnabled()) {
        log.tracev("Initializing object from ResultSet: {0}", MessageHelper.infoString(concreteEntityPersister, id, context.getSession().getFactory()));
    }
    // add temp entry so that the next step is circular-reference
    // safe - only needed because some types don't take proper
    // advantage of two-phase-load (esp. components)
    TwoPhaseLoad.addUninitializedEntity(entityKey, entityInstance, concreteEntityPersister, lockModeToAcquire, context.getSession());
    final EntityPersister rootEntityPersister = context.getSession().getFactory().getMetamodel().entityPersister(concreteEntityPersister.getRootEntityName());
    final Object[] values;
    try {
        values = concreteEntityPersister.hydrate(resultSet, id, entityInstance, (Loadable) entityReference.getEntityPersister(), concreteEntityPersister == rootEntityPersister ? entityReferenceAliases.getColumnAliases().getSuffixedPropertyAliases() : entityReferenceAliases.getColumnAliases().getSuffixedPropertyAliases(concreteEntityPersister), context.getLoadPlan().areLazyAttributesForceFetched(), context.getSession());
        context.getProcessingState(entityReference).registerHydratedState(values);
    } catch (SQLException e) {
        throw context.getSession().getFactory().getServiceRegistry().getService(JdbcServices.class).getSqlExceptionHelper().convert(e, "Could not read entity state from ResultSet : " + entityKey);
    }
    final Object rowId;
    try {
        rowId = concreteEntityPersister.hasRowId() ? resultSet.getObject(entityReferenceAliases.getColumnAliases().getRowIdAlias()) : null;
    } catch (SQLException e) {
        throw context.getSession().getFactory().getServiceRegistry().getService(JdbcServices.class).getSqlExceptionHelper().convert(e, "Could not read entity row-id from ResultSet : " + entityKey);
    }
    final EntityType entityType = EntityFetch.class.isInstance(entityReference) ? ((EntityFetch) entityReference).getFetchedType() : entityReference.getEntityPersister().getEntityMetamodel().getEntityType();
    if (entityType != null) {
        String ukName = entityType.getRHSUniqueKeyPropertyName();
        if (ukName != null) {
            final int index = ((UniqueKeyLoadable) concreteEntityPersister).getPropertyIndex(ukName);
            final Type type = concreteEntityPersister.getPropertyTypes()[index];
            // polymorphism not really handled completely correctly,
            // perhaps...well, actually its ok, assuming that the
            // entity name used in the lookup is the same as the
            // the one used here, which it will be
            EntityUniqueKey euk = new EntityUniqueKey(entityReference.getEntityPersister().getEntityName(), ukName, type.semiResolve(values[index], context.getSession(), entityInstance), type, concreteEntityPersister.getEntityMode(), context.getSession().getFactory());
            context.getSession().getPersistenceContext().addEntity(euk, entityInstance);
        }
    }
    TwoPhaseLoad.postHydrate(concreteEntityPersister, id, values, rowId, entityInstance, lockModeToAcquire, context.getSession());
    context.registerHydratedEntity(entityReference, entityKey, entityInstance);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) Loadable(org.hibernate.persister.entity.Loadable) Serializable(java.io.Serializable) SQLException(java.sql.SQLException) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) UniqueKeyLoadable(org.hibernate.persister.entity.UniqueKeyLoadable) EntityType(org.hibernate.type.EntityType) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityType(org.hibernate.type.EntityType) VersionType(org.hibernate.type.VersionType) Type(org.hibernate.type.Type) EntityUniqueKey(org.hibernate.engine.spi.EntityUniqueKey)

Example 25 with EntityType

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

the class AbstractCollectionReference method buildIndexGraph.

private CollectionFetchableIndex buildIndexGraph() {
    final CollectionPersister persister = collectionQuerySpace.getCollectionPersister();
    if (persister.hasIndex()) {
        final Type type = persister.getIndexType();
        if (type.isAssociationType()) {
            if (type.isEntityType()) {
                final EntityPersister indexPersister = persister.getFactory().getEntityPersister(((EntityType) type).getAssociatedEntityName());
                final ExpandingEntityQuerySpace entityQuerySpace = QuerySpaceHelper.INSTANCE.makeEntityQuerySpace(collectionQuerySpace, indexPersister, CollectionPropertyNames.COLLECTION_INDICES, (EntityType) persister.getIndexType(), collectionQuerySpace.getExpandingQuerySpaces().generateImplicitUid(), collectionQuerySpace.canJoinsBeRequired(), allowIndexJoin);
                return new CollectionFetchableIndexEntityGraph(this, entityQuerySpace);
            } else if (type.isAnyType()) {
                return new CollectionFetchableIndexAnyGraph(this);
            }
        } else if (type.isComponentType()) {
            final ExpandingCompositeQuerySpace compositeQuerySpace = QuerySpaceHelper.INSTANCE.makeCompositeQuerySpace(collectionQuerySpace, new CompositePropertyMapping((CompositeType) persister.getIndexType(), (PropertyMapping) persister, ""), CollectionPropertyNames.COLLECTION_INDICES, (CompositeType) persister.getIndexType(), collectionQuerySpace.getExpandingQuerySpaces().generateImplicitUid(), collectionQuerySpace.canJoinsBeRequired(), allowIndexJoin);
            return new CollectionFetchableIndexCompositeGraph(this, compositeQuerySpace);
        }
    }
    return null;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ExpandingCompositeQuerySpace(org.hibernate.loader.plan.build.spi.ExpandingCompositeQuerySpace) EntityType(org.hibernate.type.EntityType) CompositeType(org.hibernate.type.CompositeType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CompositePropertyMapping(org.hibernate.loader.plan.build.internal.spaces.CompositePropertyMapping) ExpandingEntityQuerySpace(org.hibernate.loader.plan.build.spi.ExpandingEntityQuerySpace) CompositeType(org.hibernate.type.CompositeType)

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