Search in sources :

Example 1 with ResultSetMappingImpl

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

the class ResultSetMappingProcessor method generateResultMapping.

public ResultSetMapping generateResultMapping(boolean queryHadAliases) {
    if (!queryHadAliases) {
        return this.resultSetMapping;
    }
    final ResultSetMappingImpl resultSetMapping = new ResultSetMappingImpl(null);
    final Set<String> visited = new HashSet<>();
    this.resultSetMapping.visitResultBuilders((i, resultBuilder) -> {
        if (resultBuilder instanceof NativeQuery.RootReturn) {
            final NativeQuery.RootReturn rootReturn = (NativeQuery.RootReturn) resultBuilder;
            final String suffix = alias2Suffix.get(rootReturn.getTableAlias());
            visited.add(rootReturn.getTableAlias());
            if (suffix == null) {
                resultSetMapping.addResultBuilder(resultBuilder);
            } else {
                final DynamicResultBuilderEntityStandard resultBuilderEntity = createSuffixedResultBuilder(rootReturn, suffix);
                resultSetMapping.addResultBuilder(resultBuilderEntity);
                alias2Return.put(rootReturn.getTableAlias(), resultBuilderEntity);
            }
        } else if (resultBuilder instanceof NativeQuery.CollectionReturn) {
            final NativeQuery.CollectionReturn collectionReturn = (NativeQuery.CollectionReturn) resultBuilder;
            final String suffix = alias2CollectionSuffix.get(collectionReturn.getTableAlias());
            if (suffix == null) {
                resultSetMapping.addResultBuilder(resultBuilder);
            } else {
                final CompleteResultBuilderCollectionStandard resultBuilderCollection = createSuffixedResultBuilder(collectionReturn, suffix, alias2Suffix.get(collectionReturn.getTableAlias()));
                resultSetMapping.addResultBuilder(resultBuilderCollection);
                alias2Return.put(collectionReturn.getTableAlias(), resultBuilderCollection);
            }
        } else {
            resultSetMapping.addResultBuilder(resultBuilder);
        }
    });
    this.resultSetMapping.visitLegacyFetchBuilders(fetchBuilder -> applyFetchBuilder(resultSetMapping, fetchBuilder, visited));
    return resultSetMapping;
}
Also used : CompleteResultBuilderCollectionStandard(org.hibernate.query.results.complete.CompleteResultBuilderCollectionStandard) ResultSetMappingImpl(org.hibernate.query.results.ResultSetMappingImpl) NativeQuery(org.hibernate.query.NativeQuery) DynamicResultBuilderEntityStandard(org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard) HashSet(java.util.HashSet)

Example 2 with ResultSetMappingImpl

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

the class NamedProcedureCallDefinitionImpl method resolve.

@Override
public NamedCallableQueryMemento resolve(SessionFactoryImplementor sessionFactory) {
    final Set<String> collectedQuerySpaces = new HashSet<>();
    final boolean specifiesResultClasses = resultClasses != null && resultClasses.length > 0;
    final boolean specifiesResultSetMappings = resultSetMappings != null && resultSetMappings.length > 0;
    ResultSetMappingImpl resultSetMapping = new ResultSetMappingImpl(registeredName);
    if (specifiesResultClasses) {
        Util.resolveResultSetMappingClasses(resultClasses, resultSetMapping, collectedQuerySpaces::add, new ResultSetMappingResolutionContext() {

            @Override
            public SessionFactoryImplementor getSessionFactory() {
                return sessionFactory;
            }
        });
    } else if (specifiesResultSetMappings) {
        Util.resolveResultSetMappingNames(resultSetMappings, resultSetMapping, collectedQuerySpaces::add, new ResultSetMappingResolutionContext() {

            @Override
            public SessionFactoryImplementor getSessionFactory() {
                return sessionFactory;
            }
        });
    }
    return new NamedCallableQueryMementoImpl(getRegistrationName(), procedureName, parameterDefinitions.getParameterStrategy(), parameterDefinitions.toMementos(sessionFactory), resultSetMappings, resultClasses, collectedQuerySpaces, false, null, CacheMode.IGNORE, FlushMode.AUTO, false, null, null, null, hints);
}
Also used : ResultSetMappingImpl(org.hibernate.query.results.ResultSetMappingImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ResultSetMappingResolutionContext(org.hibernate.query.internal.ResultSetMappingResolutionContext) NamedCallableQueryMementoImpl(org.hibernate.procedure.internal.NamedCallableQueryMementoImpl) HashSet(java.util.HashSet)

Example 3 with ResultSetMappingImpl

use of org.hibernate.query.results.ResultSetMappingImpl 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 4 with ResultSetMappingImpl

use of org.hibernate.query.results.ResultSetMappingImpl 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

ResultSetMappingImpl (org.hibernate.query.results.ResultSetMappingImpl)4 ResultSetMappingResolutionContext (org.hibernate.query.internal.ResultSetMappingResolutionContext)3 HashSet (java.util.HashSet)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 NamedResultSetMappingMemento (org.hibernate.query.named.NamedResultSetMappingMemento)2 ResultSetMapping (org.hibernate.query.results.ResultSetMapping)2 StringReader (java.io.StringReader)1 Configuration (org.hibernate.cfg.Configuration)1 ReaderInputStream (org.hibernate.engine.jdbc.ReaderInputStream)1 NamedCallableQueryMementoImpl (org.hibernate.procedure.internal.NamedCallableQueryMementoImpl)1 NativeQuery (org.hibernate.query.NativeQuery)1 NamedObjectRepository (org.hibernate.query.named.NamedObjectRepository)1 ResultBuilderBasicValued (org.hibernate.query.results.ResultBuilderBasicValued)1 CompleteResultBuilderCollectionStandard (org.hibernate.query.results.complete.CompleteResultBuilderCollectionStandard)1 CompleteResultBuilderEntityValued (org.hibernate.query.results.complete.CompleteResultBuilderEntityValued)1 DynamicResultBuilderEntityStandard (org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard)1 QueryEngine (org.hibernate.query.spi.QueryEngine)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1