use of org.hibernate.query.sql.spi.NamedNativeQueryMemento 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.sql.spi.NamedNativeQueryMemento 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.sql.spi.NamedNativeQueryMemento 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.sql.spi.NamedNativeQueryMemento in project hibernate-orm by hibernate.
the class NamedObjectRepositoryImpl method resolve.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Prepare repository for use
@Override
public NamedQueryMemento resolve(SessionFactoryImplementor sessionFactory, MetadataImplementor bootMetamodel, String registrationName) {
NamedQueryMemento namedQuery = sqlMementoMap.get(registrationName);
if (namedQuery != null) {
return namedQuery;
}
namedQuery = sqmMementoMap.get(registrationName);
if (namedQuery != null) {
return namedQuery;
}
namedQuery = callableMementoMap.get(registrationName);
if (namedQuery != null) {
return namedQuery;
}
final NamedHqlQueryDefinition namedHqlQueryDefinition = bootMetamodel.getNamedHqlQueryMapping(registrationName);
if (namedHqlQueryDefinition != null) {
final NamedSqmQueryMemento resolved = namedHqlQueryDefinition.resolve(sessionFactory);
sqmMementoMap.put(namedHqlQueryDefinition.getRegistrationName(), resolved);
return resolved;
}
final NamedNativeQueryDefinition namedNativeQueryDefinition = bootMetamodel.getNamedNativeQueryMapping(registrationName);
if (namedNativeQueryDefinition != null) {
final NamedNativeQueryMemento resolved = namedNativeQueryDefinition.resolve(sessionFactory);
sqlMementoMap.put(namedNativeQueryDefinition.getRegistrationName(), resolved);
return resolved;
}
final NamedProcedureCallDefinition namedCallableQueryDefinition = bootMetamodel.getNamedProcedureCallMapping(registrationName);
if (namedCallableQueryDefinition != null) {
final NamedCallableQueryMemento resolved = namedCallableQueryDefinition.resolve(sessionFactory);
callableMementoMap.put(namedCallableQueryDefinition.getRegistrationName(), resolved);
return resolved;
}
return null;
}
use of org.hibernate.query.sql.spi.NamedNativeQueryMemento in project hibernate-orm by hibernate.
the class AbstractSharedSessionContract method buildNamedQuery.
@SuppressWarnings("rawtypes")
protected QueryImplementor buildNamedQuery(String queryName, Function<NamedSqmQueryMemento, SqmQueryImplementor> namedSqmHandler, Function<NamedNativeQueryMemento, NativeQueryImplementor> namedNativeHandler) {
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 NamedObjectRepository namedObjectRepository = getFactory().getQueryEngine().getNamedObjectRepository();
final NamedSqmQueryMemento namedSqmQueryMemento = namedObjectRepository.getSqmQueryMemento(queryName);
if (namedSqmQueryMemento != null) {
return namedSqmHandler.apply(namedSqmQueryMemento);
}
// otherwise, see if it is a named native query
final NamedNativeQueryMemento namedNativeDescriptor = namedObjectRepository.getNativeQueryMemento(queryName);
if (namedNativeDescriptor != null) {
return namedNativeHandler.apply(namedNativeDescriptor);
}
throw new UnknownNamedQueryException(queryName);
}
Aggregations