Search in sources :

Example 1 with NamedSQLQueryDefinition

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;
}
Also used : NamedSQLQueryDefinition(org.hibernate.engine.NamedSQLQueryDefinition) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) WrappingQuery(org.xwiki.query.WrappingQuery) SecureQuery(org.xwiki.query.SecureQuery) WrappingQuery(org.xwiki.query.WrappingQuery) SQLQuery(org.hibernate.SQLQuery)

Example 2 with NamedSQLQueryDefinition

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());
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) NamedSQLQueryDefinition(org.hibernate.engine.NamedSQLQueryDefinition) DefaultQuery(org.xwiki.query.internal.DefaultQuery) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) DefaultQuery(org.xwiki.query.internal.DefaultQuery) WrappingQuery(org.xwiki.query.WrappingQuery) HibernateSessionFactory(com.xpn.xwiki.store.hibernate.HibernateSessionFactory) SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

SQLQuery (org.hibernate.SQLQuery)2 NamedSQLQueryDefinition (org.hibernate.engine.NamedSQLQueryDefinition)2 Query (org.xwiki.query.Query)2 WrappingQuery (org.xwiki.query.WrappingQuery)2 HibernateSessionFactory (com.xpn.xwiki.store.hibernate.HibernateSessionFactory)1 Session (org.hibernate.Session)1 Test (org.junit.Test)1 QueryFilter (org.xwiki.query.QueryFilter)1 SecureQuery (org.xwiki.query.SecureQuery)1 DefaultQuery (org.xwiki.query.internal.DefaultQuery)1