title: Creating multiple object with one response using Spring FrameWork
body: <p>This is my first time using <code>Spring</code> to add and get data from a <code>database</code>. I have successfully established connection with my <code>database</code> and can add <code>Plants</code> with only <code>id</code> and <code>plantName</code> but now for the part that I do not understand. <br>
  How can I create an <code>RowMapper</code> or something else that would work with a loop that would get the <code>Plant</code> and go over all <code>PlantParts</code> to get me the object I need.
  <br>
  I should note that <code>Plant</code> is just an Object I created to ask here to keep it cleaner and easier to answer.</p>
  
  <pre><code>@Data
  @NoArgsConstructor
  @AllArgsConstructor
  public class Plant {
      private Long id;
      private String plantName;
      private List&lt;PlantParts&gt; plantParts;
  
      public void add(PlantParts plantPartsObj) {
          if (plantParts == null) {
              plantParts = new ArrayList&lt;&gt;();
          }
  
          plantParts.add(plantPartsObj);
      }
  }
  
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  public class PlantParts {
      private String leaves;
      private String pedicle;
      private String petals;
  }
  </code></pre>
  
  <p>First I create a <code>Plant</code> object with this query:</p>
  
  <pre><code>INSERT INTO PLANTS (id, plantname)
    VALUES (NEXT VALUE FOR sequence, ?);
  </code></pre>
  
  <p>Then I add multiple <code>PlantParts</code> with this query:</p>
  
  <pre><code>INSERT INTO PLANT_PARTS (id, leaves, pedicle, petals)" +
                          "  VALUES (?, ?, ?, ?);
  </code></pre>
  
  <p>And now I am trying to make it all into one <code>Plant</code> object, but I do not get how I can get multiple <code>PlantParts</code> with one <code>RowMapper</code>.</p>
  
  <p>And this would be my code using <code>Spring</code>:</p>
  
  <pre><code>@Repository
  public class PlantDao {
  
      @Autowired
      private JdbcTemplate template;
  
      @Override
      public Order getPlantById(Long id) {
          String sql = "SELECT plants.id, plants.plantName, plant_parts.id, plant_parts.leaves, plant_parts.pedicle, plant_parts.petals " +
                  "FROM plants " +
                  "LEFT JOIN plant_parts ON plants.id = plant_parts.id AND plants.id = ?";
  
          return template.queryForObject(sql, new Object[] {id}, getPostRowMapper());
  
      }
  
      private RowMapper&lt;Plant&gt; getPostRowMapper() {
          return (rs, rowNum) -&gt; new Plant(
                  rs.getLong("id"),
                  rs.getString("plantname")
  
                  //Check if there are any PlantParts, if yes then go over
                  //all of the PlantParts in a loop
  
          });
      }
  </code></pre>
  
  <p>Now the part that I do not understand is commented out. How can I go over all <code>PlantParts</code> in the <code>RowMapper</code>.</p>
  
  <p>I would like the <code>RowMapper</code> to do something like this after setting <code>id and plantname</code>:</p>
  
  <pre><code>//For the first line
  if(rs.getString("leaves") != null){
      plant.add(new PlantRow(rs.getString("leaves"),
              rs.getInt("pedicle"), rs.getInt("petals")));
  }
  while (rs.next()){
      if(rs.getString("leaves") != null){
          plant.add(new PlantRow(rs.getString("leaves"),
                  rs.getInt("pedicle"), rs.getInt("petals")));
      }
  }
  </code></pre>
  
  <p>I do not mind if there is another way to do it, without using <code>RowMapper</code>, I just need something that would make it work.</p>