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;
}
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;
}
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);
}
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);
}
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;
}
Aggregations