use of org.hibernate.loader.plan.spi.EntityReference in project hibernate-orm by hibernate.
the class AbstractRowReader method resolveEntityKey.
private void resolveEntityKey(ResultSet resultSet, ResultSetProcessingContextImpl context, EntityReferenceInitializer entityReferenceInitializer) throws SQLException {
final EntityReference entityReference = entityReferenceInitializer.getEntityReference();
final EntityIdentifierDescription identifierDescription = entityReference.getIdentifierDescription();
if (identifierDescription.hasFetches() || identifierDescription.hasBidirectionalEntityReferences()) {
resolveEntityKey(resultSet, context, (FetchSource) identifierDescription);
}
entityReferenceInitializer.resolveEntityKey(resultSet, context);
}
use of org.hibernate.loader.plan.spi.EntityReference in project hibernate-orm by hibernate.
the class ResultSetProcessingContextImpl method createSubselects.
private void createSubselects() {
if (subselectLoadableEntityKeyMap == null || nRowsRead <= 1) {
LOG.tracef("Skipping create subselects because there are fewer than 2 results, so query by key is more efficient.", getClass().getName());
// early return
return;
}
final Map<String, int[]> namedParameterLocMap = ResultSetProcessorHelper.buildNamedParameterLocMap(queryParameters, namedParameterContext);
final String subselectQueryString = SubselectFetch.createSubselectFetchQueryFragment(queryParameters);
for (Map.Entry<EntityReference, Set<EntityKey>> entry : subselectLoadableEntityKeyMap.entrySet()) {
if (!entry.getKey().getEntityPersister().hasSubselectLoadableCollections()) {
continue;
}
SubselectFetch subselectFetch = new SubselectFetch(subselectQueryString, aliasResolutionContext.resolveSqlTableAliasFromQuerySpaceUid(entry.getKey().getQuerySpaceUid()), (Loadable) entry.getKey().getEntityPersister(), queryParameters, entry.getValue(), namedParameterLocMap);
for (EntityKey key : entry.getValue()) {
session.getPersistenceContext().getBatchFetchQueue().addSubselect(key, subselectFetch);
}
}
}
use of org.hibernate.loader.plan.spi.EntityReference in project hibernate-orm by hibernate.
the class ResultSetProcessingContextImpl method getProcessingState.
@Override
public EntityReferenceProcessingState getProcessingState(final EntityReference entityReference) {
if (identifierResolutionContextMap == null) {
identifierResolutionContextMap = new IdentityHashMap<>();
}
EntityReferenceProcessingState context = identifierResolutionContextMap.get(entityReference);
if (context == null) {
context = new EntityReferenceProcessingState() {
private boolean wasMissingIdentifier;
private Object identifierHydratedForm;
private EntityKey entityKey;
private Object[] hydratedState;
private Object entityInstance;
@Override
public EntityReference getEntityReference() {
return entityReference;
}
@Override
public void registerMissingIdentifier() {
if (!EntityFetch.class.isInstance(entityReference)) {
throw new IllegalStateException("Missing return row identifier");
}
ResultSetProcessingContextImpl.this.registerNonExists((EntityFetch) entityReference);
wasMissingIdentifier = true;
}
@Override
public boolean isMissingIdentifier() {
return wasMissingIdentifier;
}
@Override
public void registerIdentifierHydratedForm(Object identifierHydratedForm) {
this.identifierHydratedForm = identifierHydratedForm;
}
@Override
public Object getIdentifierHydratedForm() {
return identifierHydratedForm;
}
@Override
public void registerEntityKey(EntityKey entityKey) {
this.entityKey = entityKey;
}
@Override
public EntityKey getEntityKey() {
return entityKey;
}
@Override
public void registerHydratedState(Object[] hydratedState) {
this.hydratedState = hydratedState;
}
@Override
public Object[] getHydratedState() {
return hydratedState;
}
@Override
public void registerEntityInstance(Object entityInstance) {
this.entityInstance = entityInstance;
}
@Override
public Object getEntityInstance() {
return entityInstance;
}
};
identifierResolutionContextMap.put(entityReference, context);
}
return context;
}
use of org.hibernate.loader.plan.spi.EntityReference in project hibernate-orm by hibernate.
the class AbstractLoadPlanBuildingAssociationVisitationStrategy method startingEntityIdentifier.
// entity identifiers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public void startingEntityIdentifier(EntityIdentifierDefinition entityIdentifierDefinition) {
log.tracef("%s Starting entity identifier : %s", StringHelper.repeat(">>", fetchSourceStack.size()), entityIdentifierDefinition.getEntityDefinition().getEntityPersister().getEntityName());
final EntityReference entityReference = (EntityReference) currentSource();
// perform some stack validation
if (!entityReference.getEntityPersister().equals(entityIdentifierDefinition.getEntityDefinition().getEntityPersister())) {
throw new WalkingException(String.format("Encountered unexpected fetch owner [%s] in stack while processing entity identifier for [%s]", entityReference.getEntityPersister().getEntityName(), entityIdentifierDefinition.getEntityDefinition().getEntityPersister().getEntityName()));
}
if (ExpandingEntityIdentifierDescription.class.isInstance(entityReference.getIdentifierDescription())) {
pushToStack((ExpandingEntityIdentifierDescription) entityReference.getIdentifierDescription());
}
}
use of org.hibernate.loader.plan.spi.EntityReference in project hibernate-orm by hibernate.
the class LoadPlanBuilderTest method testCollectionInitializerCase.
@Test
public void testCollectionInitializerCase() {
CollectionPersister cp = sessionFactory().getCollectionPersister(Poster.class.getName() + ".messages");
FetchStyleLoadPlanBuildingAssociationVisitationStrategy strategy = new FetchStyleLoadPlanBuildingAssociationVisitationStrategy(sessionFactory(), LoadQueryInfluencers.NONE, LockMode.NONE);
LoadPlan plan = MetamodelDrivenLoadPlanBuilder.buildRootCollectionLoadPlan(strategy, cp);
assertFalse(plan.hasAnyScalarReturns());
assertEquals(1, plan.getReturns().size());
Return rtn = plan.getReturns().get(0);
CollectionReturn collectionReturn = ExtraAssertions.assertTyping(CollectionReturn.class, rtn);
assertNotNull(collectionReturn.getElementGraph());
assertNotNull(collectionReturn.getElementGraph().getFetches());
// the collection Message elements are fetched, but Message.poster is not fetched
// (because that collection is owned by that Poster)
assertEquals(0, collectionReturn.getElementGraph().getFetches().length);
EntityReference entityReference = ExtraAssertions.assertTyping(EntityReference.class, collectionReturn.getElementGraph());
assertNotNull(entityReference.getFetches());
assertEquals(0, entityReference.getFetches().length);
LoadPlanTreePrinter.INSTANCE.logTree(plan, new AliasResolutionContextImpl(sessionFactory()));
}
Aggregations