Search in sources :

Example 1 with ResultSetMapping

use of org.hibernate.query.results.ResultSetMapping in project hibernate-orm by hibernate.

the class NamedRepoTests method testMappingResolution.

@Test
public void testMappingResolution(SessionFactoryScope sessionFactoryScope) {
    final QueryEngine queryEngine = sessionFactoryScope.getSessionFactory().getQueryEngine();
    final NamedObjectRepository namedObjectRepository = queryEngine.getNamedObjectRepository();
    final NamedResultSetMappingMemento mappingMemento = namedObjectRepository.getResultSetMappingMemento("name");
    final ResultSetMapping mapping = new ResultSetMappingImpl("test");
    final ResultSetMappingResolutionContext resolutionContext = new ResultSetMappingResolutionContext() {

        @Override
        public SessionFactoryImplementor getSessionFactory() {
            return sessionFactoryScope.getSessionFactory();
        }
    };
    mappingMemento.resolve(mapping, querySpace -> {
    }, resolutionContext);
    assertThat(mapping.getNumberOfResultBuilders(), is(1));
    mapping.visitResultBuilders((position, builder) -> {
        assertThat(position, is(0));
        assertThat(builder, instanceOf(ResultBuilderBasicValued.class));
    });
}
Also used : ResultBuilderBasicValued(org.hibernate.query.results.ResultBuilderBasicValued) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) ResultSetMapping(org.hibernate.query.results.ResultSetMapping) ResultSetMappingImpl(org.hibernate.query.results.ResultSetMappingImpl) NamedResultSetMappingMemento(org.hibernate.query.named.NamedResultSetMappingMemento) ResultSetMappingResolutionContext(org.hibernate.query.internal.ResultSetMappingResolutionContext) QueryEngine(org.hibernate.query.spi.QueryEngine) Test(org.junit.jupiter.api.Test)

Example 2 with ResultSetMapping

use of org.hibernate.query.results.ResultSetMapping in project hibernate-orm by hibernate.

the class OutputsImpl method extractResults.

protected List extractResults(ResultSet resultSet) {
    final DirectResultSetAccess resultSetAccess = new DirectResultSetAccess(context.getSession(), jdbcStatement, resultSet);
    final ProcedureCallImpl procedureCall = (ProcedureCallImpl) context;
    final ResultSetMapping resultSetMapping = procedureCall.getResultSetMapping();
    final JavaTypeRegistry javaTypeRegistry = context.getSession().getTypeConfiguration().getJavaTypeRegistry();
    procedureCall.getParameterBindings().visitBindings((parameterImplementor, queryParameterBinding) -> {
        ProcedureParameter parameter = (ProcedureParameter) parameterImplementor;
        if (parameter.getMode() == ParameterMode.INOUT) {
            final JavaType<?> basicType = javaTypeRegistry.getDescriptor(parameterImplementor.getParameterType());
            if (basicType != null) {
                resultSetMapping.addResultBuilder(new ScalarDomainResultBuilder<>(basicType));
            } else {
                throw new NotYetImplementedFor6Exception(getClass());
            }
        }
    });
    final ExecutionContext executionContext = new ExecutionContext() {

        private final Callback callback = new CallbackImpl();

        @Override
        public SharedSessionContractImplementor getSession() {
            return OutputsImpl.this.context.getSession();
        }

        @Override
        public QueryOptions getQueryOptions() {
            return new QueryOptionsAdapter() {

                @Override
                public Boolean isReadOnly() {
                    return false;
                }
            };
        }

        @Override
        public String getQueryIdentifier(String sql) {
            return sql;
        }

        @Override
        public QueryParameterBindings getQueryParameterBindings() {
            return QueryParameterBindings.NO_PARAM_BINDINGS;
        }

        @Override
        public Callback getCallback() {
            return callback;
        }
    };
    final JdbcValues jdbcValues = new JdbcValuesResultSetImpl(resultSetAccess, null, null, this.context.getQueryOptions(), resultSetMapping.resolve(resultSetAccess, getSessionFactory()), null, executionContext);
    final RowReader<Object[]> rowReader = (RowReader<Object[]>) ResultsHelper.createRowReader(executionContext, null, RowTransformerPassThruImpl.INSTANCE, jdbcValues);
    /*
		 * Processing options effectively are only used for entity loading.  Here we don't need these values.
		 */
    final JdbcValuesSourceProcessingOptions processingOptions = new JdbcValuesSourceProcessingOptions() {

        @Override
        public Object getEffectiveOptionalObject() {
            return null;
        }

        @Override
        public String getEffectiveOptionalEntityName() {
            return null;
        }

        @Override
        public Serializable getEffectiveOptionalId() {
            return null;
        }

        @Override
        public boolean shouldReturnProxies() {
            return true;
        }
    };
    final JdbcValuesSourceProcessingStateStandardImpl jdbcValuesSourceProcessingState = new JdbcValuesSourceProcessingStateStandardImpl(executionContext, processingOptions, executionContext::registerLoadingEntityEntry);
    try {
        final RowProcessingStateStandardImpl rowProcessingState = new RowProcessingStateStandardImpl(jdbcValuesSourceProcessingState, executionContext, rowReader, jdbcValues);
        final List results = new ArrayList<>();
        while (rowProcessingState.next()) {
            results.add(rowReader.readRow(rowProcessingState, processingOptions));
            rowProcessingState.finishRowProcessing();
        }
        return results;
    } finally // catch (SQLException e) {
    // throw context.getSession().getExceptionConverter().convert( e, "Error processing return rows" );
    // }
    {
        rowReader.finishUp(jdbcValuesSourceProcessingState);
        jdbcValuesSourceProcessingState.finishUp();
        jdbcValues.finishUp(this.context.getSession());
    }
}
Also used : ProcedureParameter(org.hibernate.query.procedure.ProcedureParameter) RowReader(org.hibernate.sql.results.spi.RowReader) JdbcValuesSourceProcessingOptions(org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingOptions) JdbcValues(org.hibernate.sql.results.jdbc.spi.JdbcValues) CallbackImpl(org.hibernate.sql.exec.internal.CallbackImpl) ArrayList(java.util.ArrayList) JdbcValuesResultSetImpl(org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) Callback(org.hibernate.sql.exec.spi.Callback) ResultSetMapping(org.hibernate.query.results.ResultSetMapping) DirectResultSetAccess(org.hibernate.sql.results.jdbc.internal.DirectResultSetAccess) JdbcValuesSourceProcessingStateStandardImpl(org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl) QueryOptionsAdapter(org.hibernate.query.spi.QueryOptionsAdapter) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) ArrayList(java.util.ArrayList) List(java.util.List) ProcedureCallImpl(org.hibernate.procedure.internal.ProcedureCallImpl) RowProcessingStateStandardImpl(org.hibernate.sql.results.internal.RowProcessingStateStandardImpl)

Example 3 with ResultSetMapping

use of org.hibernate.query.results.ResultSetMapping in project hibernate-orm by hibernate.

the class QueryReturnTest method testQueryReturn.

@Test
public void testQueryReturn() {
    Configuration cfg = new Configuration();
    cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
    cfg.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
    cfg.addInputStream(new ReaderInputStream(new StringReader(QUERY_RETURN_HBM_XML)));
    SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) cfg.buildSessionFactory();
    try {
        NamedResultSetMappingMemento mappingMemento = sessionFactory.getQueryEngine().getNamedObjectRepository().getResultSetMappingMemento("myQuery");
        Assert.assertNotNull(mappingMemento);
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // NYI
        final ResultSetMapping mapping = new ResultSetMappingImpl("myQuery");
        final ResultSetMappingResolutionContext resolutionContext = new ResultSetMappingResolutionContext() {

            @Override
            public SessionFactoryImplementor getSessionFactory() {
                return sessionFactory;
            }
        };
        mappingMemento.resolve(mapping, querySpace -> {
        }, resolutionContext);
        Assert.assertEquals(1, mapping.getNumberOfResultBuilders());
        mapping.visitResultBuilders((i, resultBuilder) -> {
            Assert.assertTrue(resultBuilder instanceof CompleteResultBuilderEntityValued);
            CompleteResultBuilderEntityValued myQueryRootReturn = (CompleteResultBuilderEntityValued) resultBuilder;
            // Assert.assertEquals( "e", myQueryRootReturn.getTableAlias() );
            Assert.assertEquals("org.hibernate.orm.test.hbm.query.QueryReturnTest$Bar", myQueryRootReturn.getReferencedPart().getEntityName());
        });
    } finally {
        sessionFactory.close();
    }
}
Also used : CompleteResultBuilderEntityValued(org.hibernate.query.results.complete.CompleteResultBuilderEntityValued) ReaderInputStream(org.hibernate.engine.jdbc.ReaderInputStream) Configuration(org.hibernate.cfg.Configuration) ResultSetMapping(org.hibernate.query.results.ResultSetMapping) ResultSetMappingImpl(org.hibernate.query.results.ResultSetMappingImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) StringReader(java.io.StringReader) NamedResultSetMappingMemento(org.hibernate.query.named.NamedResultSetMappingMemento) ResultSetMappingResolutionContext(org.hibernate.query.internal.ResultSetMappingResolutionContext) Test(org.junit.Test)

Aggregations

ResultSetMapping (org.hibernate.query.results.ResultSetMapping)3 ResultSetMappingResolutionContext (org.hibernate.query.internal.ResultSetMappingResolutionContext)2 NamedResultSetMappingMemento (org.hibernate.query.named.NamedResultSetMappingMemento)2 ResultSetMappingImpl (org.hibernate.query.results.ResultSetMappingImpl)2 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)1 Configuration (org.hibernate.cfg.Configuration)1 ReaderInputStream (org.hibernate.engine.jdbc.ReaderInputStream)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 ProcedureCallImpl (org.hibernate.procedure.internal.ProcedureCallImpl)1 NamedObjectRepository (org.hibernate.query.named.NamedObjectRepository)1 ProcedureParameter (org.hibernate.query.procedure.ProcedureParameter)1 ResultBuilderBasicValued (org.hibernate.query.results.ResultBuilderBasicValued)1 CompleteResultBuilderEntityValued (org.hibernate.query.results.complete.CompleteResultBuilderEntityValued)1 QueryEngine (org.hibernate.query.spi.QueryEngine)1 QueryOptionsAdapter (org.hibernate.query.spi.QueryOptionsAdapter)1 CallbackImpl (org.hibernate.sql.exec.internal.CallbackImpl)1 Callback (org.hibernate.sql.exec.spi.Callback)1