Search in sources :

Example 11 with QueryParameters

use of org.hibernate.engine.spi.QueryParameters in project hibernate-orm by hibernate.

the class CriteriaLoader method scroll.

public ScrollableResultsImplementor scroll(SharedSessionContractImplementor session, ScrollMode scrollMode) throws HibernateException {
    QueryParameters qp = translator.getQueryParameters();
    qp.setScrollMode(scrollMode);
    return scroll(qp, resultTypes, null, session);
}
Also used : QueryParameters(org.hibernate.engine.spi.QueryParameters)

Example 12 with QueryParameters

use of org.hibernate.engine.spi.QueryParameters in project hibernate-orm by hibernate.

the class AbstractProducedQuery method getQueryParameters.

public QueryParameters getQueryParameters() {
    final HQLQueryPlan entityGraphHintedQueryPlan;
    if (entityGraphQueryHint == null) {
        entityGraphHintedQueryPlan = null;
    } else {
        queryParameterBindings.verifyParametersBound(false);
        // todo : ideally we'd update the instance state related to queryString but that is final atm
        final String expandedQuery = queryParameterBindings.expandListValuedParameters(getQueryString(), getProducer());
        entityGraphHintedQueryPlan = new HQLQueryPlan(expandedQuery, false, getProducer().getLoadQueryInfluencers().getEnabledFilters(), getProducer().getFactory(), entityGraphQueryHint);
    }
    QueryParameters queryParameters = new QueryParameters(getPositionalParameterTypes(), getPositionalParameterValues(), getNamedParameterMap(), getLockOptions(), queryOptions, true, isReadOnly(), cacheable, cacheRegion, comment, dbHints, null, optionalObject, optionalEntityName, optionalId, resultTransformer);
    queryParameters.setQueryPlan(entityGraphHintedQueryPlan);
    if (passDistinctThrough != null) {
        queryParameters.setPassDistinctThrough(passDistinctThrough);
    }
    return queryParameters;
}
Also used : HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) QueryParameters(org.hibernate.engine.spi.QueryParameters)

Example 13 with QueryParameters

use of org.hibernate.engine.spi.QueryParameters in project hibernate-orm by hibernate.

the class EncapsulatedCompositeAttributeResultSetProcessorTest method getResults.

private List<?> getResults(EntityPersister entityPersister) {
    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();
        }
    });
    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) 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) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session)

Example 14 with QueryParameters

use of org.hibernate.engine.spi.QueryParameters in project hibernate-orm by hibernate.

the class EncapsulatedCompositeIdResultSetProcessorTest method testSimpleCompositeId.

@Test
public void testSimpleCompositeId() throws Exception {
    // create some test data
    Session session = openSession();
    session.beginTransaction();
    Parent parent = new Parent();
    parent.id = new ParentPK();
    parent.id.firstName = "Joe";
    parent.id.lastName = "Blow";
    session.save(parent);
    session.getTransaction().commit();
    session.close();
    session = openSession();
    session.beginTransaction();
    Parent parentGotten = (Parent) session.get(Parent.class, parent.id);
    assertEquals(parent, parentGotten);
    session.getTransaction().commit();
    session.close();
    final List results = getResults(sessionFactory().getEntityPersister(Parent.class.getName()), new Callback() {

        @Override
        public void bind(PreparedStatement ps) throws SQLException {
            ps.setString(1, "Joe");
            ps.setString(2, "Blow");
        }

        @Override
        public QueryParameters getQueryParameters() {
            return new QueryParameters();
        }
    });
    assertEquals(1, results.size());
    Object result = results.get(0);
    assertNotNull(result);
    Parent parentWork = ExtraAssertions.assertTyping(Parent.class, result);
    assertEquals(parent, parentWork);
    // clean up test data
    session = openSession();
    session.beginTransaction();
    session.createQuery("delete Parent").executeUpdate();
    session.getTransaction().commit();
    session.close();
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement) QueryParameters(org.hibernate.engine.spi.QueryParameters) Session(org.hibernate.Session) Test(org.junit.Test)

Example 15 with QueryParameters

use of org.hibernate.engine.spi.QueryParameters 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)

Aggregations

QueryParameters (org.hibernate.engine.spi.QueryParameters)35 List (java.util.List)20 ArrayList (java.util.ArrayList)17 SQLException (java.sql.SQLException)14 PreparedStatement (java.sql.PreparedStatement)10 Session (org.hibernate.Session)10 Type (org.hibernate.type.Type)10 Test (org.junit.Test)9 EntityPersister (org.hibernate.persister.entity.EntityPersister)8 Connection (java.sql.Connection)7 ResultSet (java.sql.ResultSet)7 Work (org.hibernate.jdbc.Work)7 ResultSetProcessor (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor)7 NamedParameterContext (org.hibernate.loader.plan.exec.query.spi.NamedParameterContext)7 LoadQueryDetails (org.hibernate.loader.plan.exec.spi.LoadQueryDetails)7 LoadPlan (org.hibernate.loader.plan.spi.LoadPlan)7 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 AssociationType (org.hibernate.type.AssociationType)5 Serializable (java.io.Serializable)4 EntityType (org.hibernate.type.EntityType)4