Search in sources :

Example 1 with EntityType

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

the class DefaultLoadEventListener method checkIdClass.

private void checkIdClass(final EntityPersister persister, final LoadEvent event, final LoadEventListener.LoadType loadType, final Class idClass) {
    // is part of its generally goofy "derived identity" "feature"
    if (persister.getEntityMetamodel().getIdentifierProperty().isEmbedded()) {
        final EmbeddedComponentType dependentIdType = (EmbeddedComponentType) persister.getEntityMetamodel().getIdentifierProperty().getType();
        if (dependentIdType.getSubtypes().length == 1) {
            final Type singleSubType = dependentIdType.getSubtypes()[0];
            if (singleSubType.isEntityType()) {
                final EntityType dependentParentType = (EntityType) singleSubType;
                final Type dependentParentIdType = dependentParentType.getIdentifierOrUniqueKeyType(event.getSession().getFactory());
                if (dependentParentIdType.getReturnedClass().isInstance(event.getEntityId())) {
                    // yep that's what we have...
                    loadByDerivedIdentitySimplePkValue(event, loadType, persister, dependentIdType, event.getSession().getFactory().getEntityPersister(dependentParentType.getAssociatedEntityName()));
                    return;
                }
            }
        }
    }
    throw new TypeMismatchException("Provided id of the wrong type for class " + persister.getEntityName() + ". Expected: " + idClass + ", got " + event.getEntityId().getClass());
}
Also used : EntityType(org.hibernate.type.EntityType) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) EntityType(org.hibernate.type.EntityType) EventType(org.hibernate.event.spi.EventType) Type(org.hibernate.type.Type) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) TypeMismatchException(org.hibernate.TypeMismatchException)

Example 2 with EntityType

use of org.hibernate.type.EntityType 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 3 with EntityType

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

the class MapKeyEntityFromElement method buildKeyJoin.

public static MapKeyEntityFromElement buildKeyJoin(FromElement collectionFromElement) {
    final HqlSqlWalker walker = collectionFromElement.getWalker();
    final SessionFactoryHelper sfh = walker.getSessionFactoryHelper();
    final SessionFactoryImplementor sf = sfh.getFactory();
    final QueryableCollection collectionPersister = collectionFromElement.getQueryableCollection();
    final Type indexType = collectionPersister.getIndexType();
    if (indexType == null) {
        throw new IllegalArgumentException("Given collection is not indexed");
    }
    if (!indexType.isEntityType()) {
        throw new IllegalArgumentException("Given collection does not have an entity index");
    }
    final EntityType indexEntityType = (EntityType) indexType;
    final EntityPersister indexEntityPersister = (EntityPersister) indexEntityType.getAssociatedJoinable(sf);
    final String rhsAlias = walker.getAliasGenerator().createName(indexEntityPersister.getEntityName());
    final boolean useThetaJoin = collectionFromElement.getJoinSequence().isThetaStyle();
    MapKeyEntityFromElement join = new MapKeyEntityFromElement(useThetaJoin);
    join.initialize(HqlSqlTokenTypes.JOIN_FRAGMENT, ((Joinable) indexEntityPersister).getTableName());
    join.initialize(collectionFromElement.getWalker());
    join.initializeEntity(collectionFromElement.getFromClause(), indexEntityPersister.getEntityName(), indexEntityPersister, indexEntityType, "<map-key-join-" + collectionFromElement.getClassAlias() + ">", rhsAlias);
    //		String[] joinColumns = determineJoinColuns( collectionPersister, joinTableAlias );
    // todo : assumes columns, no formulas
    String[] joinColumns = collectionPersister.getIndexColumnNames(collectionFromElement.getCollectionTableAlias());
    JoinSequence joinSequence = sfh.createJoinSequence(useThetaJoin, indexEntityType, rhsAlias, //				JoinType.INNER_JOIN,
    collectionFromElement.getJoinSequence().getFirstJoin().getJoinType(), joinColumns);
    join.setJoinSequence(joinSequence);
    join.setOrigin(collectionFromElement, true);
    join.setColumns(joinColumns);
    join.setUseFromFragment(collectionFromElement.useFromFragment());
    join.setUseWhereFragment(collectionFromElement.useWhereFragment());
    walker.addQuerySpaces(indexEntityPersister.getQuerySpaces());
    return join;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SessionFactoryHelper(org.hibernate.hql.internal.ast.util.SessionFactoryHelper) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) EntityType(org.hibernate.type.EntityType) JoinType(org.hibernate.sql.JoinType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) HqlSqlWalker(org.hibernate.hql.internal.ast.HqlSqlWalker) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 4 with EntityType

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

the class JoinWalker method isJoinedFetchEnabledInMapping.

/**
	 * Does the mapping, and Hibernate default semantics, specify that
	 * this association should be fetched by outer joining
	 */
protected boolean isJoinedFetchEnabledInMapping(FetchMode config, AssociationType type) throws MappingException {
    if (!type.isEntityType() && !type.isCollectionType()) {
        return false;
    } else {
        if (config == FetchMode.JOIN) {
            return true;
        }
        if (config == FetchMode.SELECT) {
            return false;
        }
        if (type.isEntityType()) {
            //TODO: look at the owning property and check that it 
            //      isn't lazy (by instrumentation)
            EntityType entityType = (EntityType) type;
            EntityPersister persister = getFactory().getEntityPersister(entityType.getAssociatedEntityName());
            return !persister.hasProxy();
        } else {
            return false;
        }
    }
}
Also used : EntityType(org.hibernate.type.EntityType) EntityPersister(org.hibernate.persister.entity.EntityPersister)

Example 5 with EntityType

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

the class Loader method registerNonExists.

/**
	 * For missing objects associated by one-to-one with another object in the
	 * result set, register the fact that the the object is missing with the
	 * session.
	 */
private void registerNonExists(final EntityKey[] keys, final Loadable[] persisters, final SharedSessionContractImplementor session) {
    final int[] owners = getOwners();
    if (owners != null) {
        EntityType[] ownerAssociationTypes = getOwnerAssociationTypes();
        for (int i = 0; i < keys.length; i++) {
            int owner = owners[i];
            if (owner > -1) {
                EntityKey ownerKey = keys[owner];
                if (keys[i] == null && ownerKey != null) {
                    final PersistenceContext persistenceContext = session.getPersistenceContext();
                    /*final boolean isPrimaryKey;
						final boolean isSpecialOneToOne;
						if ( ownerAssociationTypes == null || ownerAssociationTypes[i] == null ) {
							isPrimaryKey = true;
							isSpecialOneToOne = false;
						}
						else {
							isPrimaryKey = ownerAssociationTypes[i].getRHSUniqueKeyPropertyName()==null;
							isSpecialOneToOne = ownerAssociationTypes[i].getLHSPropertyName()!=null;
						}*/
                    //TODO: can we *always* use the "null property" approach for everything?
                    /*if ( isPrimaryKey && !isSpecialOneToOne ) {
							persistenceContext.addNonExistantEntityKey(
									new EntityKey( ownerKey.getIdentifier(), persisters[i], session.getEntityMode() )
							);
						}
						else if ( isSpecialOneToOne ) {*/
                    boolean isOneToOneAssociation = ownerAssociationTypes != null && ownerAssociationTypes[i] != null && ownerAssociationTypes[i].isOneToOne();
                    if (isOneToOneAssociation) {
                        persistenceContext.addNullProperty(ownerKey, ownerAssociationTypes[i].getPropertyName());
                    }
                /*}
						else {
							persistenceContext.addNonExistantEntityUniqueKey( new EntityUniqueKey(
									persisters[i].getEntityName(),
									ownerAssociationTypes[i].getRHSUniqueKeyPropertyName(),
									ownerKey.getIdentifier(),
									persisters[owner].getIdentifierType(),
									session.getEntityMode()
							) );
						}*/
                }
            }
        }
    }
}
Also used : EntityType(org.hibernate.type.EntityType) EntityKey(org.hibernate.engine.spi.EntityKey) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

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