use of org.hibernate.query.hql.HqlTranslator in project hibernate-orm by hibernate.
the class QueryEngine method from.
public static QueryEngine from(SessionFactoryImplementor sessionFactory, MetadataImplementor metadata) {
final QueryEngineOptions queryEngineOptions = sessionFactory.getSessionFactoryOptions();
final SqmCreationOptions sqmCreationOptions = new SqmCreationOptionsStandard(sessionFactory);
final Dialect dialect = sessionFactory.getJdbcServices().getDialect();
final HqlTranslator hqlTranslator = resolveHqlTranslator(queryEngineOptions, dialect, sessionFactory, sqmCreationOptions);
final SqmTranslatorFactory sqmTranslatorFactory = resolveSqmTranslatorFactory(queryEngineOptions, dialect);
final SqmFunctionRegistry customSqmFunctionRegistry;
if (queryEngineOptions.getCustomSqmFunctionRegistry() == null) {
final Map<String, SqmFunctionDescriptor> customSqlFunctionMap = queryEngineOptions.getCustomSqlFunctionMap();
if (customSqlFunctionMap == null || customSqlFunctionMap.isEmpty()) {
customSqmFunctionRegistry = null;
} else {
customSqmFunctionRegistry = new SqmFunctionRegistry();
customSqlFunctionMap.forEach(customSqmFunctionRegistry::register);
}
} else {
customSqmFunctionRegistry = queryEngineOptions.getCustomSqmFunctionRegistry();
}
return new QueryEngine(sessionFactory.getUuid(), sessionFactory.getName(), sessionFactory.getSessionFactoryOptions().getJpaCompliance(), () -> sessionFactory.getRuntimeMetamodels().getJpaMetamodel(), sessionFactory.getSessionFactoryOptions().getCriteriaValueHandlingMode(), sessionFactory.getSessionFactoryOptions().getPreferredSqlTypeCodeForBoolean(), metadata.buildNamedQueryRepository(sessionFactory), hqlTranslator, sqmTranslatorFactory, sessionFactory.getServiceRegistry().getService(NativeQueryInterpreter.class), buildInterpretationCache(sessionFactory::getStatistics, sessionFactory.getProperties()), metadata.getTypeConfiguration(), dialect, customSqmFunctionRegistry, sessionFactory.getServiceRegistry());
}
use of org.hibernate.query.hql.HqlTranslator 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.hql.HqlTranslator in project hibernate-orm by hibernate.
the class EntityJoinTest method testNoImpliedJoinGeneratedForEqualityComparison.
@Test
@TestForIssue(jiraKey = "HHH-11538")
public void testNoImpliedJoinGeneratedForEqualityComparison(SessionFactoryScope scope) {
final String qry = "select r.id, cust.name " + "from FinancialRecord r " + " join Customer cust on r.customer = cust" + " order by r.id";
scope.inTransaction((session) -> {
final SessionFactoryImplementor factory = scope.getSessionFactory();
final EntityMappingType customerEntityDescriptor = factory.getRuntimeMetamodels().getMappingMetamodel().findEntityDescriptor(Customer.class);
final QueryEngine queryEngine = factory.getQueryEngine();
final HqlTranslator hqlTranslator = queryEngine.getHqlTranslator();
final SqmTranslatorFactory sqmTranslatorFactory = queryEngine.getSqmTranslatorFactory();
final SqmStatement<Object> sqm = hqlTranslator.translate(qry);
final SqmTranslator<SelectStatement> selectTranslator = sqmTranslatorFactory.createSelectTranslator((SqmSelectStatement<?>) sqm, QueryOptions.NONE, DomainParameterXref.empty(), QueryParameterBindings.NO_PARAM_BINDINGS, LoadQueryInfluencers.NONE, factory, true);
final SqmTranslation<SelectStatement> sqmTranslation = selectTranslator.translate();
final SelectStatement sqlAst = sqmTranslation.getSqlAst();
final List<TableGroup> roots = sqlAst.getQuerySpec().getFromClause().getRoots();
assertThat(roots.size(), is(1));
final TableGroup rootTableGroup = roots.get(0);
assertThat(rootTableGroup.getTableGroupJoins().size(), is(1));
final TableGroupJoin tableGroupJoin = rootTableGroup.getTableGroupJoins().get(0);
assertThat(tableGroupJoin.getJoinedGroup().getModelPart(), is(customerEntityDescriptor));
});
}
Aggregations