title: How to link two objects in a jsp result?
body: <p>I have a controller that gets all the news from a database and at the same time, it counts the number of comments on each of the news, but I have a problem because even when the data is correctly pulled out of the database, the jsp does not mix each news object with its corresponding comment count...</p>
  
  <p>Here is my Controller:</p>
  
  <pre><code>@RequestMapping(value = "/viewstatus", method = RequestMethod.GET)
  public ModelAndView viewStatus(ModelAndView modelAndView, @RequestParam(name = "p", defaultValue = "1") int pageNumber) {
  
      Page&lt;StatusUpdate&gt; page = statusUpdateService.getPage(pageNumber);
  
      for(StatusUpdate statusUpdate: page){
  
          SiteUser siteUser= statusUpdate.getSiteUser();
  
          modelAndView.getModel().put("siteuser", siteUser);
  
          int countComments = commentService.countStatusComments(statusUpdate);
  
          modelAndView.getModel().put("commentscounter", countComments);
      }
  
      modelAndView.getModel().put("page", page);
  
      modelAndView.setViewName("app.viewStatus");
  
      return modelAndView;
  }
  </code></pre>
  
  <p>Here is the console result:</p>
  
  <blockquote>
    <p>!!!!!! STATUSUPDATECONTROLLER: viewOneStatus : Count del comment: 2
    !!!!! VIEWSTATUS statusUpdate: StatusUpdate [id=98, title=Title97, text=Status update 97, added=2017-06-28 12:52:04.0, siteUser=SiteUser [id=1, email=test@caveofprogramming.com, plainPassword=null, password=$2a$10$TlfLjCcq8vPZHYkWcZ4rwurnqx5/g5C.5nk3hGTdiG6/cxlx1COPq, enabled=true, firstname=Mike, surname=River, repeatPassword=null, role=ROLE_ADMIN]]
    2017-03-24 21:00:23.278 DEBUG 1080 --- [nio-8080-exec-6] org.hibernate.SQL                        : select count(comment0_.id) as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=?
    Hibernate: select count(comment0_.id) as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=?
    !!!!!! STATUSUPDATECONTROLLER: viewOneStatus : Count del comment: 1
    !!!!! VIEWSTATUS statusUpdate: StatusUpdate [id=97, title=Title96, text=Status update 96, added=2017-06-27 12:52:04.0, siteUser=SiteUser [id=1, email=test@caveofprogramming.com, plainPassword=null, password=$2a$10$TlfLjCcq8vPZHYkWcZ4rwurnqx5/g5C.5nk3hGTdiG6/cxlx1COPq, enabled=true, firstname=Mike, surname=River, repeatPassword=null, role=ROLE_ADMIN]]
    2017-03-24 21:00:23.280 DEBUG 1080 --- [nio-8080-exec-6] org.hibernate.SQL                        : select count(comment0_.id) as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=?
    Hibernate: select count(comment0_.id) as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=?
    !!!!!! STATUSUPDATECONTROLLER: viewOneStatus : Count del comment: 0
    !!!!! VIEWSTATUS statusUpdate: StatusUpdate [id=96, title=Title95, text=Status update 95, added=2017-06-26 12:52:04.0, siteUser=SiteUser [id=1, email=test@caveofprogramming.com, plainPassword=null, password=$2a$10$TlfLjCcq8vPZHYkWcZ4rwurnqx5/g5C.5nk3hGTdiG6/cxlx1COPq, enabled=true, firstname=Mike, surname=River, repeatPassword=null, role=ROLE_ADMIN]]
    2017-03-24 21:00:23.282 DEBUG 1080 --- [nio-8080-exec-6] org.hibernate.SQL                        : select count(comment0_.id) as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=?
    Hibernate: select count(comment0_.id) as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=?
    !!!!!! STATUSUPDATECONTROLLER: viewOneStatus : Count del comment: 0
    !!!!! VIEWSTATUS statusUpdate: StatusUpdate [id=95, title=Title94, text=Status update 94, added=2017-06-25 12:52:04.0, siteUser=SiteUser [id=1, email=test@caveofprogramming.com, plainPassword=null, password=$2a$10$TlfLjCcq8vPZHYkWcZ4rwurnqx5/g5C.5nk3hGTdiG6/cxlx1COPq, enabled=true, firstname=Mike, surname=River, repeatPassword=null, role=ROLE_ADMIN]]
    2017-03-24 21:00:23.284 DEBUG 1080 --- [nio-8080-exec-6] org.hibernate.SQL                        : select count(comment0_.id) as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=?
    Hibernate: select count(comment0_.id) as col_0_0_ from comments comment0_ left outer join status_update statusupda1_ on comment0_.statusupdateid=statusupda1_.id where statusupda1_.id=?
    !!!!!! STATUSUPDATECONTROLLER: viewOneStatus : Count del comment: 0</p>
  </blockquote>
  
  <p>Here is the JSP:</p>
  
  <pre><code>&lt;table class="table table-hover"&gt;                               
      &lt;c:forEach var="statusUpdate" items="${page.content}"&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;ul class="list-inline posted-info"&gt;
                      &lt;li&gt;By
                          &lt;a href="${contextRoot}/profile/${statusUpdate.siteUser.id}"&gt;
                              ${statusUpdate.siteUser.firstname}
                              ${statusUpdate.siteUser.surname}
                          &lt;/a&gt;
                      &lt;/li&gt;
                      &lt;li&gt;Posted&lt;/li&gt;
                      &lt;li&gt;
                          &lt;fmt:formatDate pattern="EEEE d MMMM y 'at' H:mm:ss" value="${statusUpdate.added}" /&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;h2&gt;
                      &lt;a href="${contextRoot}/viewonestatus/${statusUpdate.id}"&gt;${statusUpdate.title}&gt;&lt;/a&gt;
                  &lt;/h2&gt;
                   &lt;p&gt;${statusUpdate.text}&lt;/p&gt; 
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;ul class="post-shares"&gt;
                      &lt;li&gt;
                          &lt;a href="#"&gt; &lt;i class="rounded-x icon-speech"&gt;&lt;/i&gt;
                              &lt;span&gt;${commentscounter}&lt;/span&gt;
                          &lt;/a&gt;
                      &lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;&lt;i class="rounded-x icon-share"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;&lt;i class="rounded-x icon-heart"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/td&gt;
          &lt;/tr&gt;                   
      &lt;/c:forEach&gt;                                
  &lt;/table&gt;
  </code></pre>
  
  <p>And here comes the result (0 for all the comments when it is not true):</p>
  
  <p><a href="https://i.stack.imgur.com/xLqlz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xLqlz.png" alt="enter image description here"></a></p>