/** INTERNAL: Answers the session to be used for the given query. */
  protected AbstractSession getSessionForQuery(DatabaseQuery query) {
    if (query.hasSessionName()) {
      return getSessionForName(query.getSessionName());
    }

    Class queryClass;
    if (query.getDescriptor() != null) {
      queryClass = query.getDescriptor().getJavaClass();
    } else {
      queryClass = query.getReferenceClass();
      if (queryClass == null) {
        throw QueryException.unnamedQueryOnSessionBroker(query);
      }
    }
    return getSessionForClass(queryClass);
  }
 /**
  * INTERNAL: Prepare the query from the prepared query. This allows a dynamic query to prepare
  * itself directly from a prepared query instance. This is used in the JPQL parse cache to allow
  * preparsed queries to be used to prepare dynamic queries. This only copies over properties that
  * are configured through JPQL.
  */
 public void prepareFromQuery(DatabaseQuery query) {
   super.prepareFromQuery(query);
   if (query.isReadAllQuery()) {
     ReadAllQuery readQuery = (ReadAllQuery) query;
     this.containerPolicy = readQuery.containerPolicy;
     if (readQuery.hasHierarchicalExpressions()) {
       this.orderSiblingsByExpressions = readQuery.getOrderSiblingsByExpressions();
       this.connectByExpression = readQuery.getConnectByExpression();
       this.startWithExpression = readQuery.getStartWithExpression();
     }
     if (readQuery.hasBatchReadAttributes()) {
       this.batchReadAttributeExpressions = readQuery.batchReadAttributeExpressions;
       this.batchReadMappingQueries = readQuery.batchReadMappingQueries;
       this.batchReadAttributes = readQuery.batchReadAttributes;
     }
   }
 }