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;
}
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);
}
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));
});
}
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();
}
}
Aggregations