@Override
  public Query getNamedSQLQuery(String queryName) {
    errorIfClosed();
    checkTransactionSynchStatus();

    NamedSQLQueryDefinition nsqlqd = findNamedNativeQuery(queryName);
    Query query =
        new NoSQLQueryImpl(
            nsqlqd, this, factory.getQueryPlanCache().getSQLParameterMetadata(nsqlqd.getQuery()));
    query.setComment("named native query " + queryName);
    return query;
  }
  /**
   * Constructs a SQLQueryImpl given a sql query defined in the mappings.
   *
   * @param queryDef The representation of the defined <sql-query/>.
   * @param session The session to which this SQLQueryImpl belongs.
   * @param parameterMetadata Metadata about parameters found in the query.
   */
  SQLQueryImpl(
      NamedSQLQueryDefinition queryDef,
      SessionImplementor session,
      ParameterMetadata parameterMetadata) {
    super(queryDef.getQueryString(), queryDef.getFlushMode(), session, parameterMetadata);
    if (queryDef.getResultSetRef() != null) {
      ResultSetMappingDefinition definition =
          session.getFactory().getResultSetMapping(queryDef.getResultSetRef());
      if (definition == null) {
        throw new MappingException(
            "Unable to find resultset-ref definition: " + queryDef.getResultSetRef());
      }
      this.queryReturns =
          new ArrayList<NativeSQLQueryReturn>(Arrays.asList(definition.getQueryReturns()));
    } else if (queryDef.getQueryReturns() != null && queryDef.getQueryReturns().length > 0) {
      this.queryReturns =
          new ArrayList<NativeSQLQueryReturn>(Arrays.asList(queryDef.getQueryReturns()));
    } else {
      this.queryReturns = new ArrayList<NativeSQLQueryReturn>();
    }

    this.querySpaces = queryDef.getQuerySpaces();
    this.callable = queryDef.isCallable();
  }
 @Override
 public SQLQuery createSQLQuery(NamedSQLQueryDefinition namedQueryDefinition) {
   return createNativeQuery(namedQueryDefinition.getQuery());
 }