Search in sources :

Example 31 with QueryParameters

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

Example 32 with QueryParameters

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

the class AbstractProducedQuery method doScroll.

protected ScrollableResultsImplementor doScroll(ScrollMode scrollMode) {
    if (getMaxResults() == 0) {
        return EmptyScrollableResults.INSTANCE;
    }
    final String query = queryParameterBindings.expandListValuedParameters(getQueryString(), getProducer());
    QueryParameters queryParameters = getQueryParameters();
    queryParameters.setScrollMode(scrollMode);
    return getProducer().scroll(query, queryParameters);
}
Also used : QueryParameters(org.hibernate.engine.spi.QueryParameters)

Example 33 with QueryParameters

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

the class NativeQueryImpl method doScroll.

@Override
protected ScrollableResultsImplementor doScroll(ScrollMode scrollMode) {
    final NativeSQLQuerySpecification nativeSQLQuerySpecification = generateQuerySpecification();
    final QueryParameters queryParameters = getQueryParameters();
    queryParameters.setScrollMode(scrollMode);
    return getProducer().scroll(nativeSQLQuerySpecification, queryParameters);
}
Also used : QueryParameters(org.hibernate.engine.spi.QueryParameters) NativeSQLQuerySpecification(org.hibernate.engine.query.spi.sql.NativeSQLQuerySpecification)

Example 34 with QueryParameters

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

the class NativeQueryImpl method getQueryParameters.

@Override
public QueryParameters getQueryParameters() {
    final QueryParameters queryParameters = super.getQueryParameters();
    queryParameters.setCallable(callable);
    queryParameters.setAutoDiscoverScalarTypes(autoDiscoverTypes);
    if (collectionKey != null) {
        queryParameters.setCollectionKeys(new Serializable[] { collectionKey });
    }
    return queryParameters;
}
Also used : QueryParameters(org.hibernate.engine.spi.QueryParameters)

Example 35 with QueryParameters

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

the class ProcedureCallImpl method getQueryParameters.

@Override
public QueryParameters getQueryParameters() {
    final QueryParameters qp = super.getQueryParameters();
    // both of these are for documentation purposes, they are actually handled directly...
    qp.setAutoDiscoverScalarTypes(true);
    qp.setCallable(true);
    return qp;
}
Also used : QueryParameters(org.hibernate.engine.spi.QueryParameters)

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