title: How to Fetch Multiple Level One to many Nesting with Jooq
body: <p>Here is the Scenario, I have 5 tables having one to many relations with each other, I have to map result data in hierarchical manner in below given Pojos with Jooq.</p>
  <p>DB Tables are a, b, c, d, e</p>
  <pre><code>// Here are response Pojo's
  Class APojo {
    public string name;
    public List&lt;BPojo&gt; listOfB;
  
  }
  
  Class BPojo {
    public string name;
    public List&lt;CPojo&gt; listOfC;
  }
  
  Class CPojo {
    public string name;
    public List&lt;DPojo&gt; listOfD;
  
  }
  
  Class DPojo {
    public string name;
    public List&lt;EPojo&gt; listOfE;
  
  }
  
  Class EPojo {
    public string name;
  
  }
  </code></pre>
  <p><strong>Expected sample response</strong></p>
  <pre><code>{
    &quot;name&quot;:&quot;A1&quot;,
    &quot;list_of_b&quot;:[
      {
        &quot;name&quot;:&quot;A1B1&quot;,
        &quot;list_of_c&quot;:[
          {
            &quot;name&quot;:&quot;A1B1C1&quot;,
            &quot;list_of_d&quot;:[
              {
                &quot;name&quot;:&quot;A1B1C1D1&quot;,
                &quot;list_of_e&quot;:[
                  {
                    &quot;name&quot;:&quot;A1B1C1D1E1&quot;
                  },
                  {
                    &quot;name&quot;:&quot;A1B1C1D1E2&quot;
                  }
                ]
              },
              {
                &quot;name&quot;:&quot;A1B1C1D2&quot;,
                &quot;list_of_e&quot;:[
                  {
                    &quot;name&quot;:&quot;A1B1C1D2E1&quot;
                  },
                  {
                    &quot;name&quot;:&quot;A1B1C1D2E2&quot;
                  }
                ]
              }
            ]
          },
          {
            &quot;name&quot;:&quot;A1B1C2&quot;,
            &quot;list_of_d&quot;:[
              {
                &quot;name&quot;:&quot;A1B1C2D1&quot;,
                &quot;list_of_e&quot;:[
                  {
                    &quot;name&quot;:&quot;A1B1C2D1E1&quot;
                  },
                  {
                    &quot;name&quot;:&quot;A1B1C2D1E2&quot;
                  }
                ]
              },
              {
                &quot;name&quot;:&quot;A1B1C2D2&quot;,
                &quot;list_of_e&quot;:[
                  {
                    &quot;name&quot;:&quot;A1B1C2D2E1&quot;
                  },
                  {
                    &quot;name&quot;:&quot;A1B1C2D2E2&quot;
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        &quot;name&quot;:&quot;A1B2&quot;,
        &quot;list_of_c&quot;:[
          {
            &quot;name&quot;:&quot;A1B2C1&quot;,
            &quot;list_of_d&quot;:[
              {
                &quot;name&quot;:&quot;A1B2C1D1&quot;,
                &quot;list_of_e&quot;:[
                  {
                    &quot;name&quot;:&quot;A1B1C1D1&quot;
                  },
                  {
                    &quot;name&quot;:&quot;A1B1C1D2&quot;
                  }
                ]
              },
              {
                &quot;name&quot;:&quot;A1B2C1D2&quot;,
                &quot;list_of_e&quot;:[
                  {
                    &quot;name&quot;:&quot;A1B1C1D1&quot;
                  },
                  {
                    &quot;name&quot;:&quot;A1B1C1D2&quot;
                  }
                ]
              }
            ]
          },
          {
            &quot;name&quot;:&quot;A1B2C2&quot;,
            &quot;list_of_d&quot;:[
              {
                &quot;name&quot;:&quot;A1B2C2D1&quot;,
                &quot;list_of_e&quot;:[
                  {
                    &quot;name&quot;:&quot;A1B1C1D1&quot;
                  },
                  {
                    &quot;name&quot;:&quot;A1B1C1D2&quot;
                  }
                ]
              },
              {
                &quot;name&quot;:&quot;A1B2C2D2&quot;,
                &quot;list_of_e&quot;:[
                  {
                    &quot;name&quot;:&quot;A1B1C1D1&quot;
                  },
                  {
                    &quot;name&quot;:&quot;A1B1C1D2&quot;
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
  </code></pre>
  <p>I tried something like this first but It did not work because fetch groups only accepts 2 arguments</p>
  <pre><code>using(configuration()).select(A.fields())
      .select(B.fields())
      .select(C.fields())
      .select(D.fields())
      .select(E.fields())
      .from(A)
      .join(B).on(A.ID.eq(B.A_ID)
      .join(C).on(B.ID.eq(C.B_ID)
      .join(D).on(C.ID.eq(D.C_ID)
      .join(E).on(D.ID.eq(E.D_ID)
      .fetchGroups(
          r -&gt; r.into(A).into(APojo.class),
          r -&gt; r.into(B).into(BPojo.class),
          r -&gt; r.into(C).into(CPojo.class),
          r -&gt; r.into(D).into(DPojo.class),
          r -&gt; r.into(E).into(EPojo.class)
       ); 
  </code></pre>
  <p><a href="https://stackoverflow.com/questions/50192902/jooq-fetch-into-mapk-listrecord2v-w">Then I got this post</a> and tried it as given below and other 2 method given in the post, but this also did not worked because <code>Collectors.toMap</code> accepts only 2 arguments and I have to fetch 5 level hierarchical data.</p>
  <pre><code>using(configuration()).select(A.fields())
          .select(B.fields())
          .select(C.fields())
          .select(D.fields())
          .select(E.fields())
          .from(A)
          .join(B).on(A.ID.eq(B.A_ID)
          .join(C).on(B.ID.eq(C.B_ID)
          .join(D).on(C.ID.eq(D.C_ID)
          .join(E).on(D.ID.eq(E.D_ID)
          .collect(Collectors.groupingBy(
              r -&gt; r.into(A).into(APojo.class),
              Collectors.toMap(
                  r -&gt; r.into(B).into(BPojo.class),
                  r -&gt; r.into(C).into(CPojo.class)
                  r -&gt; r.into(D).into(DPojo.class)
                  r -&gt; r.into(E).into(EPojo.class)
   )));
  </code></pre>