title: How to select multiple rows from multiple tables in spring jdbctemplate
body: <p>Here i want to collect multiple rows from multiple tables so i want to set the values to the beans.in user.java there is one <code>Set&lt;UserRole&gt;</code> so i want to set for that field also.so i writern another row mapper for that also. so in order to avoid setting for those fields is there any other ways to set them.</p>
  <p><strong>User.java</strong></p>
  <p>public class User implements Serializable{</p>
  <pre><code>    private Integer id;
      private String username;
      private String password;
      private Set&lt;UserRole&gt; userRoles = new HashSet&lt;UserRole&gt;(0);
  
      public User() {
      }
  </code></pre>
  <p><strong>UserRole.java</strong></p>
  <pre><code>public class UserRole implements Serializable{
      private Integer id;
      private User user;
      private Role role;
  
      public UserRole() {
      }
  </code></pre>
  <p><strong>Role.java</strong></p>
  <pre><code>public class Role implements Serializable{
  
      private Integer roleId;
      private String roleName;
      private Set&lt;UserRole&gt; userRoles = new HashSet&lt;UserRole&gt;(0);
      private Set&lt;RoleAction&gt; roleActions = new HashSet&lt;RoleAction&gt;(0);
  </code></pre>
  <p><strong>Action.java</strong></p>
  <pre><code>public class Action implements Serializable{
  
      private Integer id;
      private String name;
      private Set&lt;RoleAction&gt; roleActions = new HashSet&lt;RoleAction&gt;(0);
  </code></pre>
  <p><strong>Dao.java</strong></p>
  <pre><code>public User getUser(String username){
          
          List&lt;User&gt; userModelList=new ArrayList&lt;User&gt;();
          
          try{
          
          String query=&quot;select ID,USERNAME,PASSWORD from users u where u.username = ?&quot;;
          
          userModelList = jdbcTemplate.query(query, new Object[] { username },
                  new RowMapper&lt;User&gt;(){
              
                          @Override
                          public User mapRow( ResultSet resultSet,  int rownumber)
                                  throws SQLException {
                           User user=new User();
                              
                          user.setId(resultSet.getInt(&quot;ID&quot;));
                          user.setUsername(resultSet.getString(&quot;USERNAME&quot;));
                          user.setPassword(resultSet.getString(&quot;PASSWORD&quot;));
                          
                          int userid=user.getId();
                          
                          Role role=roleasd(userid);
                          
                          Set&lt;UserRole&gt; userRoles=setUserRole(role,user);
                          
                          for(UserRole ur:userRoles){
                          System.out.println(&quot;userRole details &quot;+ur.getId()+&quot;&quot;+ur.getRole().getRoleActions());
                          }
                          
                          user.setUserRoles(userRoles);
                          
                          System.out.println(&quot;the values are &quot;+user.getId()+&quot; &quot; +user.getUsername());
                          
                          return user;
                      }
  
                          private Set&lt;UserRole&gt; setUserRole(Role role, User user) {
  
                              Integer id = user.getId();
  
                              System.out.println(&quot;coming to  setuserrole id is &quot;+ id);
  
                              String query = &quot;select * from user_role where USER_ID=? &quot;;
  
                              Set&lt;UserRole&gt; userRoles = new HashSet&lt;UserRole&gt;();
  
                              jdbcTemplate.query(query, new Object[] { id },
                                      new RowMapper&lt;UserRole&gt;() {
  
                                  @Override
                                  public UserRole mapRow(ResultSet rs, int arg1)
                                          throws SQLException {
                                      UserRole userRole=new UserRole();
                                      System.out.println(&quot;**********&quot;+rs.getInt(&quot;ID&quot;));
                                      
                                      userRole.setId(rs.getInt(&quot;ID&quot;));
                                      userRole.setRole(role);
                                      userRole.setUser(user);
                                      userRoles.add(userRole);
                                      return userRole;
                                  }
                                  
                              });
                              
                              System.out.println(&quot;Size::::&quot;+userRoles.size());
                              
                              return userRoles;
                          }
                          
                          public  Role roleasd(int userid) {
                              
                              String query=&quot;select * from role where ROLE_ID = ?&quot;;
                              Role role=new Role();
                              
                              jdbcTemplate.query(query, new Object[] { userid },new RowMapper&lt;Role&gt;(){
  
                                          @Override
                                          public Role mapRow(ResultSet rs, int arg1)
                                                  throws SQLException {
                                              
                                              role.setRoleId(rs.getInt(&quot;ROLE_ID&quot;));
                                              role.setRoleName(rs.getString(&quot;ROLE_NAME&quot;));
                                              
                                              System.out.println(&quot;:::&quot;+rs.getString(&quot;ROLE_NAME&quot;));
                                              
                                              return role;
                                          }
  
                              });
                              
                              return role;
                              
                          }
              
          });
          
          }catch(Exception e){
              System.out.println(&quot; the DB error is &quot;+e.getMessage());
          }
          
          if (userModelList.size()&gt;0) {
              
              System.out.println(&quot;the list is &quot;+ userModelList.get(0).getUserRoles());
              
              return userModelList.get(0);
              
          }
              return null;
  }
      
  </code></pre>