use of org.hibernate.loader.plan.spi.EntityFetch 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.loader.plan.spi.EntityFetch in project hibernate-orm by hibernate.
the class AbstractLoadPlanBuildingAssociationVisitationStrategy method handleAssociationAttribute.
protected boolean handleAssociationAttribute(AssociationAttributeDefinition attributeDefinition) {
// todo : this seems to not be correct for one-to-one
final FetchStrategy fetchStrategy = determineFetchStrategy(attributeDefinition);
final ExpandingFetchSource currentSource = currentSource();
currentSource.validateFetchPlan(fetchStrategy, attributeDefinition);
final AssociationAttributeDefinition.AssociationNature nature = attributeDefinition.getAssociationNature();
if (nature == AssociationAttributeDefinition.AssociationNature.ANY) {
// for ANY mappings we need to build a Fetch:
// 1) fetch type is SELECT
// 2) (because the fetch cannot be a JOIN...) do not push it to the stack
// regardless of the fetch style, build the fetch
currentSource.buildAnyAttributeFetch(attributeDefinition, fetchStrategy);
return false;
} else if (nature == AssociationAttributeDefinition.AssociationNature.ENTITY) {
// regardless of the fetch style, build the fetch
EntityFetch fetch = currentSource.buildEntityAttributeFetch(attributeDefinition, fetchStrategy);
if (FetchStrategyHelper.isJoinFetched(fetchStrategy)) {
// only push to the stack if join fetched
pushToStack((ExpandingFetchSource) fetch);
return true;
} else {
return false;
}
} else {
// Collection
// regardless of the fetch style, build the fetch
CollectionAttributeFetch fetch = currentSource.buildCollectionAttributeFetch(attributeDefinition, fetchStrategy);
if (FetchStrategyHelper.isJoinFetched(fetchStrategy)) {
// only push to the stack if join fetched
pushToCollectionStack(fetch);
return true;
} else {
return false;
}
}
}
use of org.hibernate.loader.plan.spi.EntityFetch in project hibernate-orm by hibernate.
the class AbstractExpandingFetchSource method buildEntityAttributeFetch.
@Override
public EntityFetch buildEntityAttributeFetch(AssociationAttributeDefinition attributeDefinition, FetchStrategy fetchStrategy) {
final ExpandingEntityQuerySpace entityQuerySpace = QuerySpaceHelper.INSTANCE.makeEntityQuerySpace(expandingQuerySpace(), attributeDefinition, getQuerySpaces().generateImplicitUid(), fetchStrategy);
final EntityFetch fetch = new EntityAttributeFetchImpl(this, attributeDefinition, fetchStrategy, entityQuerySpace);
addFetch(fetch);
return fetch;
}
use of org.hibernate.loader.plan.spi.EntityFetch in project hibernate-orm by hibernate.
the class ReturnGraphTreePrinter method writeFetch.
private void writeFetch(Fetch fetch, int depth, PrintWriter printWriter) {
printWriter.print(TreePrinterHelper.INSTANCE.generateNodePrefix(depth));
if (EntityFetch.class.isInstance(fetch)) {
final EntityFetch entityFetch = (EntityFetch) fetch;
printWriter.println(extractDetails(entityFetch));
writeEntityReferenceFetches(entityFetch, depth + 1, printWriter);
} else if (CompositeFetch.class.isInstance(fetch)) {
final CompositeFetch compositeFetch = (CompositeFetch) fetch;
printWriter.println(extractDetails(compositeFetch));
writeCompositeFetchFetches(compositeFetch, depth + 1, printWriter);
} else if (CollectionAttributeFetch.class.isInstance(fetch)) {
final CollectionAttributeFetch collectionFetch = (CollectionAttributeFetch) fetch;
printWriter.println(extractDetails(collectionFetch));
writeCollectionReferenceFetches(collectionFetch, depth + 1, printWriter);
}
}
use of org.hibernate.loader.plan.spi.EntityFetch in project hibernate-orm by hibernate.
the class LoadPlanBuilderTest method testSimpleBuild.
@Test
public void testSimpleBuild() {
EntityPersister ep = (EntityPersister) sessionFactory().getClassMetadata(Message.class);
FetchStyleLoadPlanBuildingAssociationVisitationStrategy strategy = new FetchStyleLoadPlanBuildingAssociationVisitationStrategy(sessionFactory(), LoadQueryInfluencers.NONE, LockMode.NONE);
LoadPlan plan = MetamodelDrivenLoadPlanBuilder.buildRootEntityLoadPlan(strategy, ep);
assertFalse(plan.hasAnyScalarReturns());
assertEquals(1, plan.getReturns().size());
Return rtn = plan.getReturns().get(0);
EntityReturn entityReturn = ExtraAssertions.assertTyping(EntityReturn.class, rtn);
assertNotNull(entityReturn.getFetches());
assertEquals(1, entityReturn.getFetches().length);
Fetch fetch = entityReturn.getFetches()[0];
EntityFetch entityFetch = ExtraAssertions.assertTyping(EntityFetch.class, fetch);
assertNotNull(entityFetch.getFetches());
assertEquals(0, entityFetch.getFetches().length);
LoadPlanTreePrinter.INSTANCE.logTree(plan, new AliasResolutionContextImpl(sessionFactory()));
}
Aggregations