Search in sources :

Example 6 with LoadPlan

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

the class EntityAssociationResultSetProcessorTest method testManyToOneEntityProcessing.

@Test
public void testManyToOneEntityProcessing() throws Exception {
    final EntityPersister entityPersister = sessionFactory().getEntityPersister(Message.class.getName());
    // create some test data
    Session session = openSession();
    session.beginTransaction();
    Message message = new Message(1, "the message");
    Poster poster = new Poster(2, "the poster");
    session.save(message);
    session.save(poster);
    message.poster = poster;
    poster.messages.add(message);
    session.getTransaction().commit();
    session.close();
    {
        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);
                ps.setInt(1, 1);
                ResultSet resultSet = ps.executeQuery();
                results.addAll(resultSetProcessor.extractResults(resultSet, (SessionImplementor) workSession, new QueryParameters(), new NamedParameterContext() {

                    @Override
                    public int[] getNamedParameterLocations(String name) {
                        return new int[0];
                    }
                }, true, false, null, null));
                resultSet.close();
                ps.close();
            }
        });
        assertEquals(1, results.size());
        Object result = results.get(0);
        assertNotNull(result);
        Message workMessage = ExtraAssertions.assertTyping(Message.class, result);
        assertEquals(1, workMessage.mid.intValue());
        assertEquals("the message", workMessage.msgTxt);
        assertTrue(Hibernate.isInitialized(workMessage.poster));
        Poster workPoster = workMessage.poster;
        assertEquals(2, workPoster.pid.intValue());
        assertEquals("the poster", workPoster.name);
        assertFalse(Hibernate.isInitialized(workPoster.messages));
        workSession.getTransaction().commit();
        workSession.close();
    }
    // clean up test data
    session = openSession();
    session.beginTransaction();
    session.createQuery("delete Message").executeUpdate();
    session.createQuery("delete Poster").executeUpdate();
    session.getTransaction().commit();
    session.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NamedParameterContext(org.hibernate.loader.plan.exec.query.spi.NamedParameterContext) ResultSetProcessor(org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) QueryParameters(org.hibernate.engine.spi.QueryParameters) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) LoadQueryDetails(org.hibernate.loader.plan.exec.spi.LoadQueryDetails) Work(org.hibernate.jdbc.Work) ResultSet(java.sql.ResultSet) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with LoadPlan

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

the class EntityWithNonLazyCollectionResultSetProcessorTest method testEntityWithSet.

@Test
public void testEntityWithSet() throws Exception {
    final EntityPersister entityPersister = sessionFactory().getEntityPersister(Person.class.getName());
    // create some test data
    Session session = openSession();
    session.beginTransaction();
    Person person = new Person();
    person.id = 1;
    person.name = "John Doe";
    person.nickNames.add("Jack");
    person.nickNames.add("Johnny");
    session.save(person);
    session.getTransaction().commit();
    session.close();
    {
        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);
                ps.setInt(1, 1);
                ResultSet resultSet = ps.executeQuery();
                results.addAll(resultSetProcessor.extractResults(resultSet, (SessionImplementor) workSession, new QueryParameters(), new NamedParameterContext() {

                    @Override
                    public int[] getNamedParameterLocations(String name) {
                        return new int[0];
                    }
                }, true, false, null, null));
                resultSet.close();
                ps.close();
            }
        });
        assertEquals(2, results.size());
        Object result1 = results.get(0);
        assertSame(result1, results.get(1));
        assertNotNull(result1);
        Person workPerson = ExtraAssertions.assertTyping(Person.class, result1);
        assertEquals(1, workPerson.id.intValue());
        assertEquals(person.name, workPerson.name);
        assertTrue(Hibernate.isInitialized(workPerson.nickNames));
        assertEquals(2, workPerson.nickNames.size());
        assertEquals(person.nickNames, workPerson.nickNames);
        workSession.getTransaction().commit();
        workSession.close();
    }
    // clean up test data
    session = openSession();
    session.beginTransaction();
    session.delete(person);
    session.getTransaction().commit();
    session.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NamedParameterContext(org.hibernate.loader.plan.exec.query.spi.NamedParameterContext) ResultSetProcessor(org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) QueryParameters(org.hibernate.engine.spi.QueryParameters) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) LoadQueryDetails(org.hibernate.loader.plan.exec.spi.LoadQueryDetails) Work(org.hibernate.jdbc.Work) ResultSet(java.sql.ResultSet) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with LoadPlan

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

the class LoadPlanStructureAssertionHelper method performBasicComparison.

/**
	 * Performs a basic comparison.  Builds a LoadPlan for the given persister and compares it against the
	 * expectations according to the Loader/Walker corollary.
	 *
	 * @param sf The SessionFactory
	 * @param persister The entity persister for which to build a LoadPlan and compare against the Loader/Walker
	 * expectations.
	 */
public void performBasicComparison(SessionFactoryImplementor sf, OuterJoinLoadable persister) {
    // todo : allow these to be passed in by tests?
    final LoadQueryInfluencers influencers = LoadQueryInfluencers.NONE;
    final LockMode lockMode = LockMode.NONE;
    final int batchSize = 1;
    // legacy Loader-based contracts...
    final EntityJoinWalker walker = new EntityJoinWalker(persister, persister.getKeyColumnNames(), batchSize, lockMode, sf, influencers);
    //		final EntityLoader loader = new EntityLoader( persister, lockMode, sf, influencers );
    LoadPlan plan = buildLoadPlan(sf, persister, influencers, lockMode);
    LoadQueryDetails details = BatchingLoadQueryDetailsFactory.INSTANCE.makeEntityLoadQueryDetails(plan, persister.getKeyColumnNames(), new QueryBuildingParametersImpl(influencers, batchSize, lockMode, null), sf);
    compare(walker, details);
}
Also used : LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) QueryBuildingParametersImpl(org.hibernate.loader.plan.exec.query.internal.QueryBuildingParametersImpl) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) LoadQueryDetails(org.hibernate.loader.plan.exec.spi.LoadQueryDetails) EntityJoinWalker(org.hibernate.loader.entity.EntityJoinWalker) LockMode(org.hibernate.LockMode)

Example 9 with LoadPlan

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

the class EntityWithNonLazyOneToManySetResultSetProcessorTest method testEntityWithSet.

@Test
public void testEntityWithSet() throws Exception {
    final EntityPersister entityPersister = sessionFactory().getEntityPersister(Poster.class.getName());
    // create some test data
    Session session = openSession();
    session.beginTransaction();
    Poster poster = new Poster();
    poster.pid = 0;
    poster.name = "John Doe";
    Message message1 = new Message();
    message1.mid = 1;
    message1.msgTxt = "Howdy!";
    message1.poster = poster;
    poster.messages.add(message1);
    Message message2 = new Message();
    message2.mid = 2;
    message2.msgTxt = "Bye!";
    message2.poster = poster;
    poster.messages.add(message2);
    session.save(poster);
    session.getTransaction().commit();
    session.close();
    session = openSession();
    session.beginTransaction();
    Poster posterGotten = (Poster) session.get(Poster.class, poster.pid);
    assertEquals(0, posterGotten.pid.intValue());
    assertEquals(poster.name, posterGotten.name);
    assertTrue(Hibernate.isInitialized(posterGotten.messages));
    assertEquals(2, posterGotten.messages.size());
    for (Message message : posterGotten.messages) {
        if (message.mid == 1) {
            assertEquals(message1.msgTxt, message.msgTxt);
        } else if (message.mid == 2) {
            assertEquals(message2.msgTxt, message.msgTxt);
        } else {
            fail("unexpected message id.");
        }
        assertSame(posterGotten, message.poster);
    }
    session.getTransaction().commit();
    session.close();
    {
        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);
                ps.setInt(1, 0);
                ResultSet resultSet = ps.executeQuery();
                results.addAll(resultSetProcessor.extractResults(resultSet, (SessionImplementor) workSession, new QueryParameters(), new NamedParameterContext() {

                    @Override
                    public int[] getNamedParameterLocations(String name) {
                        return new int[0];
                    }
                }, true, false, null, null));
                resultSet.close();
                ps.close();
            }
        });
        assertEquals(2, results.size());
        Object result1 = results.get(0);
        assertNotNull(result1);
        assertSame(result1, results.get(1));
        Poster workPoster = ExtraAssertions.assertTyping(Poster.class, result1);
        assertEquals(0, workPoster.pid.intValue());
        assertEquals(poster.name, workPoster.name);
        assertTrue(Hibernate.isInitialized(workPoster.messages));
        assertEquals(2, workPoster.messages.size());
        assertTrue(Hibernate.isInitialized(posterGotten.messages));
        assertEquals(2, workPoster.messages.size());
        for (Message message : workPoster.messages) {
            if (message.mid == 1) {
                assertEquals(message1.msgTxt, message.msgTxt);
            } else if (message.mid == 2) {
                assertEquals(message2.msgTxt, message.msgTxt);
            } else {
                fail("unexpected message id.");
            }
            assertSame(workPoster, message.poster);
        }
        workSession.getTransaction().commit();
        workSession.close();
    }
    // clean up test data
    session = openSession();
    session.beginTransaction();
    session.delete(poster);
    session.getTransaction().commit();
    session.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NamedParameterContext(org.hibernate.loader.plan.exec.query.spi.NamedParameterContext) ResultSetProcessor(org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) QueryParameters(org.hibernate.engine.spi.QueryParameters) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) LoadQueryDetails(org.hibernate.loader.plan.exec.spi.LoadQueryDetails) Work(org.hibernate.jdbc.Work) ResultSet(java.sql.ResultSet) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 10 with LoadPlan

use of org.hibernate.loader.plan.spi.LoadPlan 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()));
}
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) 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) FetchStyleLoadPlanBuildingAssociationVisitationStrategy(org.hibernate.loader.plan.build.internal.FetchStyleLoadPlanBuildingAssociationVisitationStrategy) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) Test(org.junit.Test)

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