Search in sources :

Example 6 with ResultSetProcessor

use of org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor in project hibernate-orm by hibernate.

the class EntityAssociationResultSetProcessorTest method testNestedManyToOneEntityProcessing.

@Test
public void testNestedManyToOneEntityProcessing() throws Exception {
    final EntityPersister entityPersister = sessionFactory().getEntityPersister(ReportedMessage.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);
    ReportedMessage reportedMessage = new ReportedMessage(0, "inappropriate", message);
    session.save(reportedMessage);
    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(1, results.size());
        Object result = results.get(0);
        assertNotNull(result);
        ReportedMessage workReportedMessage = ExtraAssertions.assertTyping(ReportedMessage.class, result);
        assertEquals(0, workReportedMessage.id.intValue());
        assertEquals("inappropriate", workReportedMessage.reason);
        Message workMessage = workReportedMessage.message;
        assertNotNull(workMessage);
        assertTrue(Hibernate.isInitialized(workMessage));
        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 ReportedMessage").executeUpdate();
    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 ResultSetProcessor

use of org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor in project hibernate-orm by hibernate.

the class EntityWithNonLazyOneToManyListResultSetProcessorTest method testEntityWithList.

@Test
public void testEntityWithList() 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() );
    //		assertEquals( message1.msgTxt, posterGotten.messages.get( 0 ).msgTxt );
    //		assertEquals( message2.msgTxt, posterGotten.messages.get( 1 ).msgTxt );
    //		assertSame( posterGotten, posterGotten.messages.get( 0 ).poster );
    //		assertSame( posterGotten, posterGotten.messages.get( 1 ).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(workPoster.messages));
        assertEquals(2, workPoster.messages.size());
        assertEquals(message1.msgTxt, workPoster.messages.get(0).msgTxt);
        assertEquals(message2.msgTxt, workPoster.messages.get(1).msgTxt);
        assertSame(workPoster, workPoster.messages.get(0).poster);
        assertSame(workPoster, workPoster.messages.get(1).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 8 with ResultSetProcessor

use of org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor in project hibernate-orm by hibernate.

the class NonEncapsulatedCompositeIdResultSetProcessorTest 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 {
            System.out.println("SQL : " + sql);
            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)

Example 9 with ResultSetProcessor

use of org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor in project hibernate-orm by hibernate.

the class SimpleResultSetProcessorTest method testSimpleEntityProcessing.

@Test
public void testSimpleEntityProcessing() throws Exception {
    final EntityPersister entityPersister = sessionFactory().getEntityPersister(SimpleEntity.class.getName());
    // create some test data
    Session session = openSession();
    session.beginTransaction();
    session.save(new SimpleEntity(1, "the only"));
    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 {
                ((SessionImplementor) workSession).getFactory().getServiceRegistry().getService(JdbcServices.class).getSqlStatementLogger().logStatement(sql);
                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);
        SimpleEntity workEntity = ExtraAssertions.assertTyping(SimpleEntity.class, result);
        assertEquals(1, workEntity.id.intValue());
        assertEquals("the only", workEntity.name);
        workSession.getTransaction().commit();
        workSession.close();
    }
    // clean up test data
    session = openSession();
    session.beginTransaction();
    session.createQuery("delete SimpleEntity").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)

Aggregations

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 LoadQueryDetails (org.hibernate.loader.plan.exec.spi.LoadQueryDetails)9 LoadPlan (org.hibernate.loader.plan.spi.LoadPlan)9 QueryParameters (org.hibernate.engine.spi.QueryParameters)7 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)6 Test (org.junit.Test)6 SQLException (java.sql.SQLException)3