title: How to manipulate Data Transfer Object if sql join 2 tables?
body: <p>I have a query in <strong>Data Access Object</strong> DAOComments that joins users table and comments table and then store the result into <strong>Data Transfer Object</strong> DTOComments:</p>
  
  <pre><code>private static final String SQL_FIND_WITH_USERNAME =
  "SELECT u.username, comments.* FROM users u JOIN comments 
  ON u.id = comments.id ORDER BY created_date DESC LIMIT 10;";
  </code></pre>
  
  <p>However, DTOComments does not have property "username" since it is  the property of DTOUsers. </p>
  
  <p><strong>Option 1</strong></p>
  
  <p>So I decided to use <code>Map&lt;String, DTOComments&gt;</code></p>
  
  <p>In this case the map has <code>username as KEY, DTOComments as VALUE.</code></p>
  
  <p>But this approach will fails, because I care about the ORDER of result and that's why my query returns result in descending order. If I iterate the map on JSP page, the order is not consistent, so my JSP page would output the comment in random order.</p>
  
  <p>(Even if order doesn't matter, I don't know if JSTL can display map's KEY. I know displaying the VALUE though)</p>
  
  <p><strong>Option 2</strong></p>
  
  <p>I could put the query result into <code>ArrayList&lt;DTOComments&gt;</code> </p>
  
  <p>But I don't see any room to store the <code>"username"</code> now. Maybe I can add new property to DTOComments like <code>private String username;</code></p>
  
  <p>hmm... this would violate the concept of having DTO since it <strong>SHOULD</strong> reflect the database table schema.</p>
  
  <p><strong>Option 3</strong></p>
  
  <p>Create new class that hold all the information I need (ie. username + properties of DTOComments).</p>
  
  <p>But just because I need one more property "username" in addition to the properties of DTOComments, creating new class seems not right way.</p>
  
  <p>Could anyone give me advice how can I store all info returned by the query above in more organized way?</p>