Search in sources :

Example 11 with LoadPlan

use of org.hibernate.loader.plan.spi.LoadPlan 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()));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Fetch(org.hibernate.loader.plan.spi.Fetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) AliasResolutionContextImpl(org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl) CascadeStyleLoadPlanBuildingAssociationVisitationStrategy(org.hibernate.loader.plan.build.internal.CascadeStyleLoadPlanBuildingAssociationVisitationStrategy) CollectionReturn(org.hibernate.loader.plan.spi.CollectionReturn) Return(org.hibernate.loader.plan.spi.Return) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) Test(org.junit.Test)

Example 12 with LoadPlan

use of org.hibernate.loader.plan.spi.LoadPlan 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()));
}
Also used : AliasResolutionContextImpl(org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) CollectionReturn(org.hibernate.loader.plan.spi.CollectionReturn) Return(org.hibernate.loader.plan.spi.Return) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) CollectionReturn(org.hibernate.loader.plan.spi.CollectionReturn) FetchStyleLoadPlanBuildingAssociationVisitationStrategy(org.hibernate.loader.plan.build.internal.FetchStyleLoadPlanBuildingAssociationVisitationStrategy) EntityReference(org.hibernate.loader.plan.spi.EntityReference) Test(org.junit.Test)

Example 13 with LoadPlan

use of org.hibernate.loader.plan.spi.LoadPlan in project hibernate-orm by hibernate.

the class LoadPlanStructureAssertionTest method testEncapsulatedCompositeIdWithFetches1.

@Test
public void testEncapsulatedCompositeIdWithFetches1() {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Card.class);
    cfg.addAnnotatedClass(CardField.class);
    cfg.addAnnotatedClass(Key.class);
    cfg.addAnnotatedClass(PrimaryKey.class);
    SessionFactoryImplementor sf = (SessionFactoryImplementor) cfg.buildSessionFactory();
    try {
        final OuterJoinLoadable cardFieldPersister = (OuterJoinLoadable) sf.getClassMetadata(CardField.class);
        doCompare(sf, cardFieldPersister);
        final LoadPlan loadPlan = LoadPlanStructureAssertionHelper.INSTANCE.buildLoadPlan(sf, cardFieldPersister);
        assertEquals(LoadPlan.Disposition.ENTITY_LOADER, loadPlan.getDisposition());
        assertEquals(1, loadPlan.getReturns().size());
        final EntityReturn cardFieldReturn = assertTyping(EntityReturn.class, loadPlan.getReturns().get(0));
        assertEquals(0, cardFieldReturn.getFetches().length);
        // CardField defines a composite pk with 2 many-to-ones : Card and Key (the id description acts as the composite);
        // because it is an @EmbeddedId, the ID provided by the application is used "as is"
        // and fetches are not included in the load plan.
        assertFalse(cardFieldReturn.getIdentifierDescription().hasFetches());
    // we need the readers ordered in a certain manner.  Here specifically: Fetch(Card), Fetch(Key), Return(CardField)
    //
    // additionally, we need Fetch(Card) and Fetch(Key) to be hydrated/semi-resolved beforeQuery attempting to
    // resolve the EntityKey for Return(CardField)
    //
    // together those sound like argument enough to continue keeping readers for "identifier fetches" as part of
    // a special "identifier reader".  generated aliases could help here too to remove cyclic-ness from the graph.
    // but at any rate, we need to know still when this becomes circularity
    } finally {
        sf.close();
    }
}
Also used : OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) Configuration(org.hibernate.cfg.Configuration) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CardField(org.hibernate.test.annotations.cid.keymanytoone.CardField) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) Test(org.junit.Test) EncapsulatedCompositeIdResultSetProcessorTest(org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)

Example 14 with LoadPlan

use of org.hibernate.loader.plan.spi.LoadPlan in project hibernate-orm by hibernate.

the class LoadPlanStructureAssertionTest method testEncapsulatedCompositeIdWithFetches2.

@Test
public void testEncapsulatedCompositeIdWithFetches2() {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Card.class);
    cfg.addAnnotatedClass(CardField.class);
    cfg.addAnnotatedClass(Key.class);
    cfg.addAnnotatedClass(PrimaryKey.class);
    final SessionFactoryImplementor sf = (SessionFactoryImplementor) cfg.buildSessionFactory();
    try {
        final OuterJoinLoadable cardPersister = (OuterJoinLoadable) sf.getClassMetadata(Card.class);
        doCompare(sf, cardPersister);
        final LoadPlan cardLoadPlan = LoadPlanStructureAssertionHelper.INSTANCE.buildLoadPlan(sf, cardPersister);
        assertEquals(LoadPlan.Disposition.ENTITY_LOADER, cardLoadPlan.getDisposition());
        assertEquals(1, cardLoadPlan.getReturns().size());
        // Check the root EntityReturn(Card)
        final EntityReturn cardReturn = assertTyping(EntityReturn.class, cardLoadPlan.getReturns().get(0));
        assertFalse(cardReturn.getIdentifierDescription().hasFetches());
        // Card should have one fetch, the fields collection
        assertEquals(1, cardReturn.getFetches().length);
        final CollectionAttributeFetch fieldsFetch = assertTyping(CollectionAttributeFetch.class, cardReturn.getFetches()[0]);
        assertNotNull(fieldsFetch.getElementGraph());
        // the Card.fields collection has entity elements of type CardField...
        final CollectionFetchableElementEntityGraph cardFieldElementGraph = assertTyping(CollectionFetchableElementEntityGraph.class, fieldsFetch.getElementGraph());
        // CardField should have no fetches
        assertEquals(0, cardFieldElementGraph.getFetches().length);
        // But it should have 1 key-many-to-one fetch for Key (Card is already handled)
        assertTrue(cardFieldElementGraph.getIdentifierDescription().hasFetches());
        final FetchSource cardFieldElementGraphIdAsFetchSource = assertTyping(FetchSource.class, cardFieldElementGraph.getIdentifierDescription());
        assertEquals(1, cardFieldElementGraphIdAsFetchSource.getFetches().length);
        assertEquals(1, cardFieldElementGraphIdAsFetchSource.getBidirectionalEntityReferences().length);
        BidirectionalEntityReference circularCardFetch = assertTyping(BidirectionalEntityReference.class, cardFieldElementGraphIdAsFetchSource.getBidirectionalEntityReferences()[0]);
        assertSame(circularCardFetch.getTargetEntityReference(), cardReturn);
        // the fetch above is to the other key-many-to-one for CardField.primaryKey composite: key
        EntityFetch keyFetch = assertTyping(EntityFetch.class, cardFieldElementGraphIdAsFetchSource.getFetches()[0]);
        assertEquals(Key.class.getName(), keyFetch.getEntityPersister().getEntityName());
    } finally {
        sf.close();
    }
}
Also used : EntityFetch(org.hibernate.loader.plan.spi.EntityFetch) OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) CollectionAttributeFetch(org.hibernate.loader.plan.spi.CollectionAttributeFetch) CollectionFetchableElementEntityGraph(org.hibernate.loader.plan.build.internal.returns.CollectionFetchableElementEntityGraph) FetchSource(org.hibernate.loader.plan.spi.FetchSource) Configuration(org.hibernate.cfg.Configuration) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) BidirectionalEntityReference(org.hibernate.loader.plan.spi.BidirectionalEntityReference) Key(org.hibernate.test.annotations.cid.keymanytoone.Key) PrimaryKey(org.hibernate.test.annotations.cid.keymanytoone.PrimaryKey) Card(org.hibernate.test.annotations.cid.keymanytoone.Card) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) Test(org.junit.Test) EncapsulatedCompositeIdResultSetProcessorTest(org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)

Example 15 with LoadPlan

use of org.hibernate.loader.plan.spi.LoadPlan in project hibernate-orm by hibernate.

the class EncapsulatedCompositeIdResultSetProcessorTest method getResults.

private List getResults(final EntityPersister entityPersister, final Callback callback) {
    final LoadPlan plan = Helper.INSTANCE.buildLoadPlan(sessionFactory(), entityPersister);
    final LoadQueryDetails queryDetails = Helper.INSTANCE.buildLoadQueryDetails(plan, sessionFactory());
    final String sql = queryDetails.getSqlStatement();
    final ResultSetProcessor resultSetProcessor = queryDetails.getResultSetProcessor();
    final List results = new ArrayList();
    final Session workSession = openSession();
    workSession.beginTransaction();
    workSession.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(sql);
            callback.bind(ps);
            ResultSet resultSet = ps.executeQuery();
            //callback.beforeExtractResults( workSession );
            results.addAll(resultSetProcessor.extractResults(resultSet, (SessionImplementor) workSession, callback.getQueryParameters(), new NamedParameterContext() {

                @Override
                public int[] getNamedParameterLocations(String name) {
                    return new int[0];
                }
            }, true, false, null, null));
            resultSet.close();
            ps.close();
        }
    });
    workSession.getTransaction().commit();
    workSession.close();
    return results;
}
Also used : NamedParameterContext(org.hibernate.loader.plan.exec.query.spi.NamedParameterContext) ResultSetProcessor(org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) LoadQueryDetails(org.hibernate.loader.plan.exec.spi.LoadQueryDetails) Work(org.hibernate.jdbc.Work) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session)

Aggregations

LoadPlan (org.hibernate.loader.plan.spi.LoadPlan)19 Test (org.junit.Test)15 LoadQueryDetails (org.hibernate.loader.plan.exec.spi.LoadQueryDetails)10 Connection (java.sql.Connection)9 PreparedStatement (java.sql.PreparedStatement)9 ResultSet (java.sql.ResultSet)9 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Session (org.hibernate.Session)9 Work (org.hibernate.jdbc.Work)9 ResultSetProcessor (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor)9 NamedParameterContext (org.hibernate.loader.plan.exec.query.spi.NamedParameterContext)9 EntityPersister (org.hibernate.persister.entity.EntityPersister)8 QueryParameters (org.hibernate.engine.spi.QueryParameters)7 AliasResolutionContextImpl (org.hibernate.loader.plan.exec.internal.AliasResolutionContextImpl)7 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 EntityReturn (org.hibernate.loader.plan.spi.EntityReturn)5 EntityGraph (javax.persistence.EntityGraph)4 EntityManager (javax.persistence.EntityManager)4 Join (org.hibernate.loader.plan.spi.Join)4