Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1087005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:53:03+00:00 2026-05-16T22:53:03+00:00

I have a query in Data Access Object DAOComments that joins users table and

  • 0

I have a query in Data Access Object DAOComments that joins users table and comments table and then store the result into Data Transfer Object DTOComments:

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;";

However, DTOComments does not have property “username” since it is the property of DTOUsers.

Option 1

So I decided to use Map<String, DTOComments>

In this case the map has username as KEY, DTOComments as VALUE.

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.

(Even if order doesn’t matter, I don’t know if JSTL can display map’s KEY. I know displaying the VALUE though)

Option 2

I could put the query result into ArrayList<DTOComments>

But I don’t see any room to store the "username" now. Maybe I can add new property to DTOComments like private String username;

hmm… this would violate the concept of having DTO since it SHOULD reflect the database table schema.

Option 3

Create new class that hold all the information I need (ie. username + properties of DTOComments).

But just because I need one more property “username” in addition to the properties of DTOComments, creating new class seems not right way.

Could anyone give me advice how can I store all info returned by the query above in more organized way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:53:03+00:00Added an answer on May 16, 2026 at 10:53 pm

    If I iterate the map on JSP page, the order is not consistent, so my JSP page would output the comment in random order.

    That’s the nature of HashMap. If you want to maintain insertion order in a Map, then you should be using LinkedHashMap instead. But the Map<User, Comment> approach has another disadvantage, if an user has posted more than one comment, you would be overwriting the previously inserted comment this way. You would like to use a Map<Comment, User> instead.

    But IMHO it’s better to make the User a property in Comment class, indicating a many-to-one relationship:

    public class Comment {
        private User user; // +getter +setter
    }
    

    This way you can end up with a List<Comment>.


    That said, I wanted to comment on another statement of you:

    I don’t know if JSTL can display map’s KEY. I know displaying the VALUE though)

    You can iterate over a map using <c:forEach>. It goes over Map#entrySet(). Each iteration gives a Map.Entry object back which in turn has getKey() and getValue() methods.

    Here’s a kickoff example:

    <c:forEach items="${map}" var="entry">
        Key: ${entry.key}, value: ${entry.value}<br>
    </c:forEach>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that transfers data from multiple slave tables into a single
I have a number of data access methods that accept a dynamic object parameter
I have a query that returns data with group category and some details like
I have a table from which I query data to display resultset using stored
I have this SQL Query that pulls data from 3 tables. I am unable
I have a YQL query that extracts data from a page and returns it
I have a repository layer that is responsible for my data-access, which is called
So i have a query that is suppose to get me data from two
I have following query and data saved in a MySQL database with a timestamp
I have an excel workbook which needs to query data from an HTTP server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.