if ( loadPlan.getReturns().size() == 0 ) { throw new IllegalStateException( "LoadPlan contained no root returns" ); else if ( loadPlan.getReturns().size() > 1 ) { throw new IllegalStateException( "LoadPlan contained more than one root returns" ); final Return rootReturn = loadPlan.getReturns().get( 0 ); if ( !returnType.isInstance( rootReturn ) ) { throw new IllegalStateException(
if ( queryParameters.getOptionalId() != null ) { if ( loadPlan.getReturns().size() > 1 ) { throw new IllegalStateException( "Cannot specify 'optional entity' values with multi-return load plans" );
private void logTree( LoadPlan loadPlan, AliasResolutionContext aliasResolutionContext, PrintWriter printWriter) { printWriter.println( "LoadPlan(" + extractDetails( loadPlan ) + ")" ); printWriter.println( TreePrinterHelper.INSTANCE.generateNodePrefix( 1 ) + "Returns" ); for ( Return rtn : loadPlan.getReturns() ) { ReturnGraphTreePrinter.INSTANCE.write( rtn, 2, printWriter ); printWriter.flush(); } QuerySpaceTreePrinter.INSTANCE.write( loadPlan.getQuerySpaces(), 1, aliasResolutionContext, printWriter ); printWriter.flush(); }
private String extractDetails(LoadPlan loadPlan) { switch ( loadPlan.getDisposition() ) { case MIXED: { return "mixed"; } case ENTITY_LOADER: { return "entity=" + ( (EntityReturn) loadPlan.getReturns().get( 0 ) ).getEntityPersister().getEntityName(); } case COLLECTION_INITIALIZER: { return "collection=" + ( (CollectionReturn) loadPlan.getReturns().get( 0 ) ).getCollectionPersister().getRole(); } default: { return "???"; } } } }
private void handlePotentiallyEmptyCollectionRootReturns( LoadPlan loadPlan, Serializable[] collectionKeys, ResultSet resultSet, SharedSessionContractImplementor session) { if ( collectionKeys == null ) { // this is not a collection initializer (and empty collections will be detected by looking for // the owner's identifier in the result set) return; } // this is a collection initializer, so we must create a collection // for each of the passed-in keys, to account for the possibility // that the collection is empty and has no rows in the result set // // todo : move this inside CollectionReturn ? CollectionPersister persister = ( (CollectionReturn) loadPlan.getReturns().get( 0 ) ).getCollectionPersister(); for ( Serializable key : collectionKeys ) { if ( LOG.isDebugEnabled() ) { LOG.debugf( "Preparing collection intializer : %s", MessageHelper.collectionInfoString( persister, key, session.getFactory() ) ); } session.getPersistenceContext() .getLoadContexts() .getCollectionLoadContext( resultSet ) .getLoadingCollection( persister, key ); } }
@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() ) ); }
@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() ) ); }
@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() ) ); }