use of org.hibernate.engine.NamedSQLQueryDefinition in project xwiki-platform by xwiki.
the class HqlQueryExecutor method createNamedHibernateQuery.
private org.hibernate.Query createNamedHibernateQuery(Session session, Query query) {
org.hibernate.Query hQuery = session.getNamedQuery(query.getStatement());
Query filteredQuery = query;
if (filteredQuery.getFilters() != null && !filteredQuery.getFilters().isEmpty()) {
// Since we can't modify the Hibernate query statement at this point we need to create a new one to apply
// the query filter. This comes with a performance cost, we could fix it by handling named queries ourselves
// and not delegate them to Hibernate. This way we would always get a statement that we can transform before
// the execution.
boolean isNative = hQuery instanceof SQLQuery;
String language = isNative ? "sql" : Query.HQL;
final String statement = hQuery.getQueryString();
// Run filters
filteredQuery = filterQuery(new WrappingQuery(filteredQuery) {
@Override
public String getStatement() {
return statement;
}
}, language);
if (isNative) {
hQuery = session.createSQLQuery(filteredQuery.getStatement());
// Copy the information about the return column types, if possible.
NamedSQLQueryDefinition definition = (NamedSQLQueryDefinition) this.sessionFactory.getConfiguration().getNamedSQLQueries().get(query.getStatement());
if (!StringUtils.isEmpty(definition.getResultSetRef())) {
((SQLQuery) hQuery).setResultSetMapping(definition.getResultSetRef());
}
} else {
hQuery = session.createQuery(filteredQuery.getStatement());
}
}
populateParameters(hQuery, filteredQuery);
return hQuery;
}
use of org.hibernate.engine.NamedSQLQueryDefinition in project xwiki-platform by xwiki.
the class HqlQueryExecutorTest method createNamedNativeHibernateQuery.
@Test
@SuppressWarnings("unchecked")
public void createNamedNativeHibernateQuery() throws Exception {
DefaultQuery query = new DefaultQuery("queryName", this.executor);
Session session = mock(Session.class);
SQLQuery sqlQuery = mock(SQLQuery.class);
when(session.getNamedQuery(query.getStatement())).thenReturn(sqlQuery);
when(sqlQuery.getQueryString()).thenReturn("foo");
// Add a Query Filter to verify it's called and can change the statement
QueryFilter filter = mock(QueryFilter.class);
query.addFilter(filter);
when(filter.filterStatement("foo", "sql")).thenReturn("bar");
when(filter.filterQuery(any(Query.class))).then(returnsFirstArg());
SQLQuery finalQuery = mock(SQLQuery.class);
when(session.createSQLQuery("bar")).thenReturn(finalQuery);
NamedSQLQueryDefinition definition = mock(NamedSQLQueryDefinition.class);
when(definition.getResultSetRef()).thenReturn("someResultSet");
HibernateSessionFactory sessionFactory = this.mocker.getInstance(HibernateSessionFactory.class);
sessionFactory.getConfiguration().getNamedSQLQueries().put(query.getStatement(), definition);
assertSame(finalQuery, this.executor.createHibernateQuery(session, query));
verify(finalQuery).setResultSetMapping(definition.getResultSetRef());
}
Aggregations