private Map checkNamedQueries() throws HibernateException {
    Map errors = new HashMap();

    // Check named HQL queries
    log.debug("Checking " + namedQueries.size() + " named HQL queries");
    Iterator itr = namedQueries.entrySet().iterator();
    while (itr.hasNext()) {
      final Map.Entry entry = (Map.Entry) itr.next();
      final String queryName = (String) entry.getKey();
      final NamedQueryDefinition qd = (NamedQueryDefinition) entry.getValue();
      // this will throw an error if there's something wrong.
      try {
        log.debug("Checking named query: " + queryName);
        // TODO: BUG! this currently fails for named queries for non-POJO entities
        queryPlanCache.getHQLQueryPlan(qd.getQueryString(), false, CollectionHelper.EMPTY_MAP);
      } catch (QueryException e) {
        errors.put(queryName, e);
      } catch (MappingException e) {
        errors.put(queryName, e);
      }
    }

    log.debug("Checking " + namedSqlQueries.size() + " named SQL queries");
    itr = namedSqlQueries.entrySet().iterator();
    while (itr.hasNext()) {
      final Map.Entry entry = (Map.Entry) itr.next();
      final String queryName = (String) entry.getKey();
      final NamedSQLQueryDefinition qd = (NamedSQLQueryDefinition) entry.getValue();
      // this will throw an error if there's something wrong.
      try {
        log.debug("Checking named SQL query: " + queryName);
        // TODO : would be really nice to cache the spec on the query-def so as to not have to
        // re-calc the hash;
        // currently not doable though because of the resultset-ref stuff...
        NativeSQLQuerySpecification spec;
        if (qd.getResultSetRef() != null) {
          ResultSetMappingDefinition definition =
              (ResultSetMappingDefinition) sqlResultSetMappings.get(qd.getResultSetRef());
          if (definition == null) {
            throw new MappingException(
                "Unable to find resultset-ref definition: " + qd.getResultSetRef());
          }
          spec =
              new NativeSQLQuerySpecification(
                  qd.getQueryString(), definition.getQueryReturns(), qd.getQuerySpaces());
        } else {
          spec =
              new NativeSQLQuerySpecification(
                  qd.getQueryString(), qd.getQueryReturns(), qd.getQuerySpaces());
        }
        queryPlanCache.getNativeSQLQueryPlan(spec);
      } catch (QueryException e) {
        errors.put(queryName, e);
      } catch (MappingException e) {
        errors.put(queryName, e);
      }
    }

    return errors;
  }
  public void testHqlQueryPlanWithEnabledFilter() {
    Session s = openSession();
    QueryPlanCache cache = ((SessionImplementor) s).getFactory().getQueryPlanCache();

    HQLQueryPlan plan1A = cache.getHQLQueryPlan("from Person", true, getEnabledFilters(s));
    HQLQueryPlan plan1B = cache.getHQLQueryPlan("from Person", false, getEnabledFilters(s));

    s.enableFilter("sex").setParameter("sexCode", new Character('F'));
    HQLQueryPlan plan2A = cache.getHQLQueryPlan("from Person", true, getEnabledFilters(s));
    HQLQueryPlan plan2B = cache.getHQLQueryPlan("from Person", false, getEnabledFilters(s));

    s.disableFilter("sex");
    HQLQueryPlan plan3A = cache.getHQLQueryPlan("from Person", true, getEnabledFilters(s));
    HQLQueryPlan plan3B = cache.getHQLQueryPlan("from Person", false, getEnabledFilters(s));

    s.enableFilter("sex").setParameter("sexCode", new Character('M'));
    HQLQueryPlan plan4A = cache.getHQLQueryPlan("from Person", true, getEnabledFilters(s));
    HQLQueryPlan plan4B = cache.getHQLQueryPlan("from Person", false, getEnabledFilters(s));

    assertSame(plan1A, plan3A);
    assertSame(plan1B, plan3B);
    assertSame(plan2A, plan4A);
    assertSame(plan2B, plan4B);

    assertNotSame(plan1A, plan1B);
    assertNotSame(plan1A, plan2A);
    assertNotSame(plan1A, plan2B);
    assertNotSame(plan1B, plan2A);
    assertNotSame(plan1B, plan2B);

    s.close();
  }
  public void testHqlQueryPlan() {
    Session s = openSession();
    QueryPlanCache cache = ((SessionImplementor) s).getFactory().getQueryPlanCache();
    assertTrue(getEnabledFilters(s).isEmpty());

    HQLQueryPlan plan1 = cache.getHQLQueryPlan("from Person", false, getEnabledFilters(s));
    HQLQueryPlan plan2 =
        cache.getHQLQueryPlan("from Person where name is null", false, getEnabledFilters(s));
    HQLQueryPlan plan3 =
        cache.getHQLQueryPlan("from Person where name = :name", false, getEnabledFilters(s));
    HQLQueryPlan plan4 =
        cache.getHQLQueryPlan("from Person where name = ?", false, getEnabledFilters(s));

    assertNotSame(plan1, plan2);
    assertNotSame(plan1, plan3);
    assertNotSame(plan1, plan4);
    assertNotSame(plan2, plan3);
    assertNotSame(plan2, plan4);
    assertNotSame(plan3, plan4);

    assertSame(plan1, cache.getHQLQueryPlan("from Person", false, getEnabledFilters(s)));
    assertSame(
        plan2,
        cache.getHQLQueryPlan("from Person where name is null", false, getEnabledFilters(s)));
    assertSame(
        plan3,
        cache.getHQLQueryPlan("from Person where name = :name", false, getEnabledFilters(s)));
    assertSame(
        plan4, cache.getHQLQueryPlan("from Person where name = ?", false, getEnabledFilters(s)));

    s.close();
  }
 public String[] getReturnAliases(String queryString) throws HibernateException {
   return queryPlanCache
       .getHQLQueryPlan(queryString, false, CollectionHelper.EMPTY_MAP)
       .getReturnMetadata()
       .getReturnAliases();
 }