use of org.hibernate.query.sqm.spi.NamedSqmQueryMemento in project hibernate-orm by hibernate.
the class NamedObjectRepositoryImpl method prepare.
@Override
public void prepare(SessionFactoryImplementor sessionFactory, MetadataImplementor bootMetamodel, BootstrapContext bootstrapContext) {
bootMetamodel.visitNamedHqlQueryDefinitions(namedHqlQueryDefinition -> {
final NamedSqmQueryMemento resolved = namedHqlQueryDefinition.resolve(sessionFactory);
sqmMementoMap.put(namedHqlQueryDefinition.getRegistrationName(), resolved);
});
bootMetamodel.visitNamedNativeQueryDefinitions(namedNativeQueryDefinition -> {
final NamedNativeQueryMemento resolved = namedNativeQueryDefinition.resolve(sessionFactory);
sqlMementoMap.put(namedNativeQueryDefinition.getRegistrationName(), resolved);
});
bootMetamodel.visitNamedResultSetMappingDefinition(namedResultSetMappingDefinition -> {
final NamedResultSetMappingMemento resolved = namedResultSetMappingDefinition.resolve(() -> sessionFactory);
resultSetMappingMementoMap.put(namedResultSetMappingDefinition.getRegistrationName(), resolved);
});
bootMetamodel.visitNamedProcedureCallDefinition(namedProcedureCallDefinition -> {
final NamedCallableQueryMemento resolved = namedProcedureCallDefinition.resolve(sessionFactory);
callableMementoMap.put(namedProcedureCallDefinition.getRegistrationName(), resolved);
});
}
use of org.hibernate.query.sqm.spi.NamedSqmQueryMemento in project hibernate-orm by hibernate.
the class NamedObjectRepositoryImpl method checkNamedQueries.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Named query checking
public Map<String, HibernateException> checkNamedQueries(QueryEngine queryEngine) {
Map<String, HibernateException> errors = new HashMap<>();
final HqlTranslator sqmProducer = queryEngine.getHqlTranslator();
final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache();
final boolean cachingEnabled = interpretationCache.isEnabled();
// Check named HQL queries
log.debugf("Checking %s named HQL queries", sqmMementoMap.size());
for (NamedSqmQueryMemento hqlMemento : sqmMementoMap.values()) {
try {
log.debugf("Checking named HQL query: %s", hqlMemento.getRegistrationName());
String queryString = hqlMemento.getHqlString();
interpretationCache.resolveHqlInterpretation(queryString, s -> queryEngine.getHqlTranslator().translate(queryString));
} catch (HibernateException e) {
errors.put(hqlMemento.getRegistrationName(), e);
}
}
// Check native-sql queries
log.debugf("Checking %s named SQL queries", sqlMementoMap.size());
for (NamedNativeQueryMemento memento : sqlMementoMap.values()) {
memento.validate(queryEngine);
// // this will throw an error if there's something wrong.
// try {
// log.debugf( "Checking named SQL query: %s", memento.getRegistrationName() );
// // TODO : would be really nice to cache the spec on the query-def so as to not have to re-calc the hash;
// // currently not doable though because of the resultset-ref stuff...
// NativeSQLQuerySpecification spec;
// if ( memento.getResultSetMappingName() != null ) {
// NamedResultSetMappingMemento resultSetMappingMemento = getResultSetMappingMemento( memento.getResultSetMappingName() );
// if ( resultSetMappingMemento == null ) {
// throw new MappingException( "Unable to find resultset-ref resultSetMappingMemento: " + memento.getResultSetMappingName() );
// }
// spec = new NativeSQLQuerySpecification(
// namedSQLQueryDefinition.getQueryString(),
// resultSetMappingMemento.getQueryReturns(),
// namedSQLQueryDefinition.getQuerySpaces()
// );
// }
// else {
// spec = new NativeSQLQuerySpecification(
// namedSQLQueryDefinition.getQueryString(),
// namedSQLQueryDefinition.getQueryReturns(),
// namedSQLQueryDefinition.getQuerySpaces()
// );
// }
// queryEngine.getNativeSQLQueryPlan( spec );
// }
// catch ( HibernateException e ) {
// errors.put( namedSQLQueryDefinition.getName(), e );
// }
}
return errors;
}
use of org.hibernate.query.sqm.spi.NamedSqmQueryMemento in project hibernate-orm by hibernate.
the class AbstractSharedSessionContract method createNamedSelectionQuery.
@Override
public <R> SelectionQuery<R> createNamedSelectionQuery(String queryName, Class<R> expectedResultType) {
checkOpen();
pulseTransactionCoordinator();
delayedAfterCompletion();
// this method can be called for either a named HQL query or a named native query
// first see if it is a named HQL query
final NamedSqmQueryMemento namedHqlDescriptor = getFactory().getQueryEngine().getNamedObjectRepository().getSqmQueryMemento(queryName);
if (namedHqlDescriptor != null) {
return createNamedSqmSelectionQuery(namedHqlDescriptor, expectedResultType);
}
// otherwise, see if it is a named native query
final NamedNativeQueryMemento namedNativeDescriptor = getFactory().getQueryEngine().getNamedObjectRepository().getNativeQueryMemento(queryName);
if (namedNativeDescriptor != null) {
return createNamedNativeSelectionQuery(namedNativeDescriptor, expectedResultType);
}
throw new UnknownNamedQueryException(queryName);
}
use of org.hibernate.query.sqm.spi.NamedSqmQueryMemento 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.sqm.spi.NamedSqmQueryMemento 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());
}
Aggregations