use of org.hibernate.loader.plan.spi.Fetch in project hibernate-orm by hibernate.
the class LoadQueryJoinAndFetchProcessor method processFetches.
public FetchStats processFetches(FetchSource fetchSource, SelectStatementBuilder selectStatementBuilder, ReaderCollector readerCollector) {
final FetchStatsImpl fetchStats = new FetchStatsImpl();
// what if fetchSource is a composite fetch (as it would be in the case of a key-many-to-one)?
if (EntityReference.class.isInstance(fetchSource)) {
final EntityReference fetchOwnerAsEntityReference = (EntityReference) fetchSource;
if (fetchOwnerAsEntityReference.getIdentifierDescription().hasFetches()) {
final FetchSource entityIdentifierAsFetchSource = (FetchSource) fetchOwnerAsEntityReference.getIdentifierDescription();
for (Fetch fetch : entityIdentifierAsFetchSource.getFetches()) {
processFetch(selectStatementBuilder, fetchSource, fetch, readerCollector, fetchStats);
}
}
}
processFetches(fetchSource, selectStatementBuilder, readerCollector, fetchStats);
return fetchStats;
}
use of org.hibernate.loader.plan.spi.Fetch in project hibernate-orm by hibernate.
the class LoadQueryJoinAndFetchProcessor method processEntityFetch.
private void processEntityFetch(SelectStatementBuilder selectStatementBuilder, FetchSource fetchSource, EntityFetch fetch, ReaderCollector readerCollector, FetchStatsImpl fetchStats) {
// todo : still need to think through expressing bi-directionality in the new model...
// if ( BidirectionalEntityFetch.class.isInstance( fetch ) ) {
// log.tracef( "Skipping bi-directional entity fetch [%s]", fetch );
// return;
// }
fetchStats.processingFetch(fetch);
if (!FetchStrategyHelper.isJoinFetched(fetch.getFetchStrategy())) {
// not join fetched, so nothing else to do
return;
}
// First write out the SQL SELECT fragments
final Joinable joinable = (Joinable) fetch.getEntityPersister();
EntityReferenceAliases aliases = aliasResolutionContext.resolveEntityReferenceAliases(fetch.getQuerySpaceUid());
// the null arguments here relate to many-to-many fetches
selectStatementBuilder.appendSelectClauseFragment(joinable.selectFragment(null, null, aliases.getTableAlias(), aliases.getColumnAliases().getSuffix(), null, true));
// process its identifier fetches first (building EntityReferenceInitializers for them if needed)
if (fetch.getIdentifierDescription().hasFetches()) {
final FetchSource entityIdentifierAsFetchSource = (FetchSource) fetch.getIdentifierDescription();
for (Fetch identifierFetch : entityIdentifierAsFetchSource.getFetches()) {
processFetch(selectStatementBuilder, fetch, identifierFetch, readerCollector, fetchStats);
}
}
// build an EntityReferenceInitializers for the incoming fetch itself
readerCollector.add(new EntityReferenceInitializerImpl(fetch, aliases));
// then visit each of our (non-identifier) fetches
processFetches(fetch, selectStatementBuilder, readerCollector, fetchStats);
}
use of org.hibernate.loader.plan.spi.Fetch in project hibernate-orm by hibernate.
the class AbstractRowReader method resolveEntityKey.
private void resolveEntityKey(ResultSet resultSet, ResultSetProcessingContextImpl context, FetchSource fetchSource) throws SQLException {
// Resolve any bidirectional entity references first.
for (BidirectionalEntityReference bidirectionalEntityReference : fetchSource.getBidirectionalEntityReferences()) {
final EntityReferenceInitializer targetEntityReferenceInitializer = entityInitializerByEntityReference.get(bidirectionalEntityReference.getTargetEntityReference());
resolveEntityKey(resultSet, context, targetEntityReferenceInitializer);
targetEntityReferenceInitializer.hydrateEntityState(resultSet, context);
}
for (Fetch fetch : fetchSource.getFetches()) {
if (EntityFetch.class.isInstance(fetch)) {
final EntityFetch entityFetch = (EntityFetch) fetch;
final EntityReferenceInitializer entityReferenceInitializer = entityInitializerByEntityReference.get(entityFetch);
if (entityReferenceInitializer != null) {
resolveEntityKey(resultSet, context, entityReferenceInitializer);
entityReferenceInitializer.hydrateEntityState(resultSet, context);
}
} else if (CompositeFetch.class.isInstance(fetch)) {
resolveEntityKey(resultSet, context, (CompositeFetch) fetch);
}
}
}
use of org.hibernate.loader.plan.spi.Fetch 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()));
}
use of org.hibernate.loader.plan.spi.Fetch in project hibernate-orm by hibernate.
the class LoadPlanBuilderTest method testCascadeBasedBuild.
@Test
public void testCascadeBasedBuild() {
EntityPersister ep = (EntityPersister) sessionFactory().getClassMetadata(Message.class);
CascadeStyleLoadPlanBuildingAssociationVisitationStrategy strategy = new CascadeStyleLoadPlanBuildingAssociationVisitationStrategy(CascadingActions.MERGE, 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