use of org.hibernate.query.sql.spi.NativeQueryImplementor 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));
}
use of org.hibernate.query.sql.spi.NativeQueryImplementor in project hibernate-orm by hibernate.
the class AbstractSharedSessionContract method buildNamedQuery.
@SuppressWarnings({ "rawtypes", "unchecked" })
protected <T> QueryImplementor<T> buildNamedQuery(String queryName, Class<T> resultType) {
try {
return buildNamedQuery(queryName, (memento) -> {
final SqmQueryImplementor query = memento.toQuery(this, resultType);
if (StringHelper.isEmpty(query.getComment())) {
query.setComment("dynamic query");
}
applyQuerySettingsAndHints(query);
if (memento.getLockOptions() != null) {
query.setLockOptions(memento.getLockOptions());
}
return query;
}, (memento) -> {
final NativeQueryImplementor query;
if (resultType == null) {
query = memento.toQuery(this);
} else {
query = memento.toQuery(this, resultType);
}
if (StringHelper.isEmpty(query.getComment())) {
query.setComment("dynamic native-SQL query");
}
applyQuerySettingsAndHints(query);
return query;
});
} catch (UnknownNamedQueryException e) {
// JPA expects this to mark the transaction for rollback only
transactionCoordinator.getTransactionDriverControl().markRollbackOnly();
// it also expects an IllegalArgumentException, so wrap UnknownNamedQueryException
throw new IllegalArgumentException(e.getMessage(), e);
} catch (IllegalArgumentException e) {
throw e;
} catch (RuntimeException e) {
throw getExceptionConverter().convert(e);
}
}
use of org.hibernate.query.sql.spi.NativeQueryImplementor in project hibernate-orm by hibernate.
the class NativeQueryResultBuilderTests method testConvertedAttributeBasedBuilder.
@Test
public void testConvertedAttributeBasedBuilder(SessionFactoryScope scope) {
scope.inTransaction(session -> {
final NativeQueryImplementor qry = session.createNativeQuery("select converted_gender from EntityOfBasics");
qry.addAttributeResult("converted_gender", "EntityOfBasics", "convertedGender");
final List results = qry.list();
assertThat(results.size(), is(1));
final Object result = results.get(0);
assertThat(result, instanceOf(EntityOfBasics.Gender.class));
assertThat(result, is(EntityOfBasics.Gender.OTHER));
});
}
Aggregations