Example usage for org.hibernate.engine.query.spi HQLQueryPlan getQuerySpaces

List of usage examples for org.hibernate.engine.query.spi HQLQueryPlan getQuerySpaces

Introduction

In this page you can find the example usage for org.hibernate.engine.query.spi HQLQueryPlan getQuerySpaces.

Prototype

public Set getQuerySpaces() 

Source Link

Usage

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateExtendedQuerySupport.java

License:Apache License

private List<QueryParamEntry> getQueryParamEntries(EntityManager em, List<Query> queries,
        Set<String> querySpaces) {
    SessionImplementor session = em.unwrap(SessionImplementor.class);
    SessionFactoryImplementor sfi = session.getFactory();
    List<QueryParamEntry> result = new ArrayList<QueryParamEntry>(queries.size());
    Deque<Query> queryQueue = new LinkedList<Query>(queries);

    while (queryQueue.size() > 0) {
        Query q = queryQueue.remove();
        if (q instanceof CteQueryWrapper) {
            List<Query> participatingQueries = ((CteQueryWrapper) q).getParticipatingQueries();
            for (int i = participatingQueries.size() - 1; i > -1; i--) {
                queryQueue.addFirst(participatingQueries.get(i));
            }//from ww w  . j  a  v a 2 s .com
            continue;
        }

        org.hibernate.Query hibernateQuery = q.unwrap(org.hibernate.Query.class);

        Map<String, TypedValue> namedParams = new HashMap<String, TypedValue>(
                hibernateAccess.getNamedParams(hibernateQuery));
        String queryString = hibernateAccess.expandParameterLists(session, hibernateQuery, namedParams);

        HQLQueryPlan queryPlan = sfi.getQueryPlanCache().getHQLQueryPlan(queryString, false,
                Collections.EMPTY_MAP);

        if (queryPlan.getTranslators().length > 1) {
            throw new IllegalArgumentException("No support for multiple translators yet!");
        }

        querySpaces.addAll(queryPlan.getQuerySpaces());

        QueryTranslator queryTranslator = queryPlan.getTranslators()[0];
        QueryParameters queryParameters;
        List<ParameterSpecification> specifications;

        try {
            queryParameters = hibernateAccess.getQueryParameters(hibernateQuery, namedParams);
            specifications = getField(queryTranslator, "collectedParameterSpecifications");

            // This only happens for modification queries
            if (specifications == null) {
                StatementExecutor executor = getStatementExecutor(queryTranslator);
                if (!(executor instanceof BasicExecutor)) {
                    throw new IllegalArgumentException(
                            "Using polymorphic deletes/updates with CTEs is not yet supported");
                }
                specifications = getField(executor, "parameterSpecifications");
            }
        } catch (Exception e1) {
            throw new RuntimeException(e1);
        }

        result.add(new QueryParamEntry(queryString, queryParameters, specifications));
    }

    return result;
}