use of org.hibernate.query.named.NamedObjectRepository in project hibernate-orm by hibernate.
the class Util method resolveResultSetMappingNames.
public static void resolveResultSetMappingNames(String[] resultSetMappingNames, ResultSetMapping resultSetMapping, Consumer<String> querySpaceConsumer, ResultSetMappingResolutionContext context) {
final NamedObjectRepository namedObjectRepository = context.getSessionFactory().getQueryEngine().getNamedObjectRepository();
for (String resultSetMappingName : resultSetMappingNames) {
final NamedResultSetMappingMemento memento = namedObjectRepository.getResultSetMappingMemento(resultSetMappingName);
if (memento == null) {
throw new UnknownSqlResultSetMappingException("Unknown SqlResultSetMapping [" + resultSetMappingName + "]");
}
memento.resolve(resultSetMapping, querySpaceConsumer, context);
}
}
use of org.hibernate.query.named.NamedObjectRepository 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.named.NamedObjectRepository in project hibernate-orm by hibernate.
the class SimpleNamedQueryTests method testBinding.
@Test
public void testBinding(SessionFactoryScope scope) {
final NamedObjectRepository namedObjectRepository = scope.getSessionFactory().getQueryEngine().getNamedObjectRepository();
final NamedSqmQueryMemento simpleMemento = namedObjectRepository.getSqmQueryMemento("simple");
assertThat(simpleMemento, notNullValue());
final NamedSqmQueryMemento restrictedMemento = namedObjectRepository.getSqmQueryMemento("restricted");
assertThat(restrictedMemento, notNullValue());
}
use of org.hibernate.query.named.NamedObjectRepository in project hibernate-orm by hibernate.
the class SimpleNamedQueryTests method testExecution.
@Test
public void testExecution(SessionFactoryScope scope) {
scope.inTransaction(session -> {
session.createNamedQuery("simple").list();
session.createNamedQuery("restricted").setParameter("name", "a name").list();
});
final NamedObjectRepository namedObjectRepository = scope.getSessionFactory().getQueryEngine().getNamedObjectRepository();
final NamedSqmQueryMemento simpleMemento = namedObjectRepository.getSqmQueryMemento("simple");
assertThat(simpleMemento, notNullValue());
final NamedSqmQueryMemento restrictedMemento = namedObjectRepository.getSqmQueryMemento("restricted");
assertThat(restrictedMemento, notNullValue());
}
use of org.hibernate.query.named.NamedObjectRepository in project hibernate-orm by hibernate.
the class SessionFactoryImpl method addNamedQuery.
@Override
public void addNamedQuery(String name, Query query) {
validateNotClosed();
// NOTE : we use Query#unwrap here (rather than direct type checking) to account for possibly wrapped
// query implementations
// first, handle StoredProcedureQuery
final NamedObjectRepository namedObjectRepository = getQueryEngine().getNamedObjectRepository();
try {
final ProcedureCallImplementor<?> unwrapped = query.unwrap(ProcedureCallImplementor.class);
if (unwrapped != null) {
namedObjectRepository.registerCallableQueryMemento(name, unwrapped.toMemento(name));
return;
}
} catch (PersistenceException ignore) {
// this means 'query' is not a ProcedureCallImplementor
}
// then try as a native-SQL or JPQL query
try {
QueryImplementor<?> hibernateQuery = query.unwrap(QueryImplementor.class);
if (hibernateQuery != null) {
// create and register the proper NamedQueryDefinition...
if (hibernateQuery instanceof NativeQueryImplementor) {
namedObjectRepository.registerNativeQueryMemento(name, ((NativeQueryImplementor<?>) hibernateQuery).toMemento(name));
} else {
final NamedQueryMemento namedQueryMemento = ((SqmQueryImplementor<?>) hibernateQuery).toMemento(name);
namedObjectRepository.registerSqmQueryMemento(name, (NamedSqmQueryMemento) namedQueryMemento);
}
return;
}
} catch (PersistenceException ignore) {
// this means 'query' is not a native-SQL or JPQL query
}
// if we get here, we are unsure how to properly unwrap the incoming query to extract the needed information
throw new PersistenceException(String.format("Unsure how to properly unwrap given Query [%s] as basis for named query", query));
}
Aggregations