=== modified file 'rules/annotations/index_selection.rules'
--- old/rules/annotations/index_selection.rules	2021-02-09 20:22:00 +0000
+++ new/rules/annotations/index_selection.rules	2022-12-22 11:22:36 +0000
@@ -114,6 +114,8 @@
 **                           Fixed conversion of sorting by extent fields with non-constant index.
 ** 042 ECF 20200906          New ORM implementation.
 ** 043 IAS 20210209          Do not use word index field for sorting
+** 044 DDF 20221222          Use hint to set related_buffer annotation to true if
+**                           in a FIND.
 */
 -->
 
@@ -187,6 +189,7 @@
    <worker class="com.goldencode.p2j.schema.SchemaWorker"            namespace="schema" />
    <worker class="com.goldencode.p2j.uast.ProgressPatternWorker"     namespace="prog" />
    <worker class="com.goldencode.p2j.convert.IndexSelectionWorker"   namespace="isw" />
+   <worker class="com.goldencode.p2j.uast.UastHintsWorker"                 namespace="uhw" />
    
    <!-- variables -->
    <variable name="record"             type="com.goldencode.ast.Aast" />
@@ -225,6 +228,8 @@
    <variable name="tableCount"         type="java.lang.Integer" />
    <variable name="countingTables"     type="java.lang.Boolean" />
    <variable name="functypes"          type="java.util.ArrayList" />
+   <variable name="inFind"             type="java.lang.Boolean" />
+   <variable name="enableFindFieldRef" type="java.lang.Boolean" />
    
    <!-- function libraries -->
    <include name="common-progress" />
@@ -350,10 +355,20 @@
       <rule>functypes.add(prog.func_raw)</rule>
       <rule>functypes.add(prog.func_memptr)</rule>
       <rule>functypes.add(prog.func_poly)</rule>
+      
+      <rule>inFind = false</rule>
    </init-rules>
    
    <walk-rules>
    
+      <!-- if the type is Find and hint is provided -->
+      <rule>type == prog.KW_FIND
+         <action>enableFindFieldRef = uhw.getUastBoolean("enable_find_field_reference")</action>
+         <rule>enableFindFieldRef != null
+            <action>inFind = enableFindFieldRef</action>
+         </rule>
+      </rule>
+   
       <!-- query-level initialization -->
       <rule>
          (type == prog.open_query or
@@ -549,7 +564,7 @@
                   <!-- in a multi-table query, remember that this field
                        is a reference back to a different but previous
                        buffer in the same query -->
-                  <rule on="false">queryBufferOrder.contains(getNoteLong("refid"))
+                  <rule on="false">queryBufferOrder.contains(getNoteLong("refid")) or inFind
                      <action>putNote("related_buffer", true)</action>
                      <action on="false">putNote("related_buffer", false)</action>
                   </rule>
@@ -611,7 +626,7 @@
                      <!-- in a multi-table query, remember that this field
                           is a reference back to a different but previous
                           buffer in the same query -->
-                     <rule>queryBufferOrder.contains(getNoteLong("refid"))
+                     <rule>queryBufferOrder.contains(getNoteLong("refid")) or inFind
                         <action>putNote("related_buffer", true)</action>
                         <action on="false">putNote("related_buffer", false)</action>
                      </rule>
@@ -1015,6 +1030,11 @@
          <action>extentSortFields.clear()</action>
       </rule>
       
+      <!-- reset flag -->
+      <rule>inFind and type == prog.KW_FIND
+         <action>inFind = false</action>
+      </rule>
+      
    </ascent-rules>
    
    <post-rules>

=== modified file 'rules/convert/expressions.rules'
--- old/rules/convert/expressions.rules	2021-07-22 19:04:41 +0000
+++ new/rules/convert/expressions.rules	2022-12-22 11:25:20 +0000
@@ -232,6 +232,7 @@
 **                           initialized at the method's execution, and not at the caller's arguments.
 **                           (backed out CA/20210428).
 **     GES 20210722          Exclude Java method calls from built-in OO 4GL processing.
+**     DDF 20221222          Include hint value if exists when the casting to P2JQuery.Parameter is inserted.
 */
 -->
 
@@ -1306,9 +1307,13 @@
                <rule>
                   getNoteBoolean("deferred") and
                   !(isNote("delegated_eval") and getNoteBoolean("delegated_eval"))
-                  <action>lastid = createJavaAst(java.cast, "P2JQuery.Parameter", lastid)</action>
-                  <action>lastid = createJavaAst(java.lambda, "", lastid)</action>
-                  <action>createJavaAst(java.lparens, "", lastid)</action>
+                  <!-- use hint if provided -->
+                  <rule>(uhw.getUastBoolean("enable_find_field_reference") != null) and
+                        uhw.getUastBoolean("enable_find_field_reference")         
+                     <action on="false">lastid = createJavaAst(java.cast, "P2JQuery.Parameter", lastid)</action>
+                     <action on="false">lastid = createJavaAst(java.lambda, "", lastid)</action>
+                     <action on="false">createJavaAst(java.lparens, "", lastid)</action>
+                  </rule>
                </rule>
                
                <!-- dynamic where subscripts in query substitutions need to

=== modified file 'src/com/goldencode/p2j/persist/FindQuery.java'
--- old/src/com/goldencode/p2j/persist/FindQuery.java	2021-09-12 01:01:10 +0000
+++ new/src/com/goldencode/p2j/persist/FindQuery.java	2022-12-22 11:27:48 +0000
@@ -63,6 +63,7 @@
 ** 024 ECF 20210806          Corrected CAN-FIND return value in event of fatal error. Fixed nested query
 **                           error detection/reporting.
 **     ECF 20210911          Javadoc fix.
+**     DDF 20221222          Added method for checking if FindQuery is nested in a FOR EACH.
 */
 
 /*
@@ -120,6 +121,7 @@
 
 package com.goldencode.p2j.persist;
 
+import java.util.*;
 import java.util.function.*;
 import com.goldencode.p2j.persist.lock.*;
 import com.goldencode.p2j.util.*;
@@ -414,7 +416,7 @@
    {
       super();
       super.initialize(dmo, where, whereExpr, sort, null, inverse, args, lockType);
-      
+      System.out.println("Current FINDQuery nested status: " + Boolean.toString(isNested()));
       // if the NO-ERROR clause is specified, then a QOEE must not be thrown
       setLenientOffEnd(getBuffer().txHelper.errHlp.isSilent());
       setErrorIfNull(true);
@@ -888,4 +890,57 @@
       
       return true;
    }
+   
+   
+   /**
+    * Check if query is nested in a FOR EACH.
+    * 
+    * @return  true if it's nested, false if not
+    */
+   private boolean isNested()
+   {
+      if (hasWhereExpression())
+      {
+         return false;
+      }
+      
+      Object[] values = getArgs();
+      
+      if (values == null)
+      {
+         return false;
+      }
+      
+      for (Object val : values)
+      {
+         if (!(val instanceof FieldReference))
+         {
+            continue;
+         }
+         
+         final FieldReference ref = (FieldReference) val;
+         RecordBuffer valueRecord = ref.getParentBuffer();
+         if (valueRecord == null)
+         {
+            continue;
+         }
+         
+         for (P2JQuery q : TransactionManager.getRelatedQueries())
+         {
+            RecordBuffer[] qBuffers = q.getRecordBuffers();
+            if (qBuffers == null)
+            {
+               continue;
+            }
+            for (RecordBuffer rb : qBuffers)
+            {
+               if (rb.equals(valueRecord))
+               {
+                  return true;
+               }
+            }
+         }
+      }
+      return false;
+   }
 }

=== modified file 'src/com/goldencode/p2j/util/BlockDefinition.java'
--- old/src/com/goldencode/p2j/util/BlockDefinition.java	2022-10-06 08:18:44 +0000
+++ new/src/com/goldencode/p2j/util/BlockDefinition.java	2022-12-22 11:39:36 +0000
@@ -56,6 +56,8 @@
 **                           processing all the scopeables for each and every block.
 **     CA  20221006          Lazy initialization for commitables and conditionListeners. 'finalizables' can be
 **                           a set instead of a list, as their order is not important.  Refs #6824
+**     DDF 20221222          Added a list for storing related queries from a block. relatedQueries are stored
+**                           only in the current scope of the block.
 */
 
 /*
@@ -116,6 +118,7 @@
 import java.util.*;
 import java.util.function.*;
 
+import com.goldencode.p2j.persist.P2JQuery;
 import com.goldencode.p2j.security.*;
 
 /**
@@ -272,6 +275,9 @@
    /** FINALLY block to be executed at the next iteration, retry or finished. */
    public Runnable finallyBlock = null;
    
+   /** List of queries from the block of a forEach */
+   public List<P2JQuery> relatedQueries = new ArrayList<>();
+   
    /**
     * Construct an instance to represent a new block.
     *

=== modified file 'src/com/goldencode/p2j/util/TransactionManager.java'
--- old/src/com/goldencode/p2j/util/TransactionManager.java	2022-10-10 09:11:45 +0000
+++ new/src/com/goldencode/p2j/util/TransactionManager.java	2022-12-22 11:43:42 +0000
@@ -511,6 +511,7 @@
 **                           a set instead of a list, as their order is not important.  Refs #6824
 **     CA  20221010          Performance improvements - avoid the ProcedureData lookup, by keeping a parallel 
 **                           stack of this data for THIS-PROCEDURE.  Refs #6826
+**     DDF 20221222          Added methods for handling relatedQueries of the BlockDefinition.
 */
 
 /*
@@ -6124,12 +6125,37 @@
             listeners.addAll(query.getOffEndListeners());
          }
          
+         BlockDefinition bd = wa.blocks.peek();
+         bd.relatedQueries.addAll(wa.offEndQueries);
          wa.offEndQueries.clear();
       }
 
       wa.offEndRegistration = false;
    }
    
+   
+   /**
+    * Clear the relatedQueries from the top block of the workArea.
+    */
+   public static void clearRelatedQueries()
+   {
+      WorkArea wa = workArea.obtain();
+      BlockDefinition bd = wa.blocks.peek();
+      bd.relatedQueries.clear();
+   }
+   
+   /**
+    * Get the relatedQueries from the top block of the workArea. 
+    * 
+    * @return  List of related queries
+    */
+   public static List<P2JQuery> getRelatedQueries()
+   {
+      WorkArea wa = workArea.obtain();
+      BlockDefinition bd = wa.blocks.peek();
+      return bd.relatedQueries;
+   }
+   
    /**
     * Register the passed query for query off-end events. The passed queries
     * are all the queries created in the init method for a FOR or FOR EACH
@@ -8773,6 +8799,7 @@
       public void popScope()
       throws StopConditionException
       {
+         TransactionManager.clearRelatedQueries();
          TransactionManager.popScope(wa);
       }
       
@@ -10450,7 +10477,7 @@
        * is overridden.
        */
       private Set<P2JQuery> offEndQueries = Collections.newSetFromMap(new IdentityHashMap<>());
-
+      
       /**
        * Flag indicating that the block executes its {@link Block#init()} 
        * method. Only variants of FOR or FOR EACH blocks will set the flag to

