Example usage for org.hibernate.engine.spi SessionFactoryImplementor getQueryPlanCache

List of usage examples for org.hibernate.engine.spi SessionFactoryImplementor getQueryPlanCache

Introduction

In this page you can find the example usage for org.hibernate.engine.spi SessionFactoryImplementor getQueryPlanCache.

Prototype

@Deprecated
QueryPlanCache getQueryPlanCache();

Source Link

Document

Access to the cachres of HQL/JPQL and native query plans.

Usage

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

License:Apache License

private HQLQueryPlan getOriginalQueryPlan(SessionImplementor session, Query query) {
    SessionFactoryImplementor sfi = session.getFactory();
    org.hibernate.Query hibernateQuery = query.unwrap(org.hibernate.Query.class);

    Map<String, TypedValue> namedParams = new HashMap<String, TypedValue>(
            hibernateAccess.getNamedParams(hibernateQuery));
    String queryString = hibernateAccess.expandParameterLists(session, hibernateQuery, namedParams);
    return sfi.getQueryPlanCache().getHQLQueryPlan(queryString, false, Collections.EMPTY_MAP);
}

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 w ww.  j av  a2 s . c  o m*/
            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;
}