java - How to manipulate Data Transfer Object if sql join 2 tables? -



java - How to manipulate Data Transfer Object if sql join 2 tables? -

i have query in data access object daocomments joins users table , comments table , store result data transfer object dtocomments:

private static final string sql_find_with_username = "select u.username, comments.* users u bring together comments on u.id = comments.id order created_date desc limit 10;";

however, dtocomments not have property "username" since property of dtousers.

option 1

so decided utilize map<string, dtocomments>

in case map has username key, dtocomments value.

but approach fails, because care order of result , that's why query returns result in descending order. if iterate map on jsp page, order not consistent, jsp page output comment in random order.

(even if order doesn't matter, don't know if jstl can display map's key. know displaying value though)

option 2

i set query result arraylist<dtocomments>

but don't see room store "username" now. maybe can add together new property dtocomments private string username;

hmm... violate concept of having dto since should reflect database table schema.

option 3

create new class hold info need (ie. username + properties of dtocomments).

but because need 1 more property "username" in add-on properties of dtocomments, creating new class seems not right way.

could give me advice how can store info returned query above in more organized way?

if iterate map on jsp page, order not consistent, jsp page output comment in random order.

that's nature of hashmap. if want maintain insertion order in map, should using linkedhashmap instead. map<user, comment> approach has disadvantage, if user has posted more 1 comment, overwriting inserted comment way. utilize map<comment, user> instead.

but imho it's improve create user property in comment class, indicating many-to-one relationship:

public class comment { private user user; // +getter +setter }

this way can end list<comment>.

that said, wanted comment on statement of you:

i don't know if jstl can display map's key. know displaying value though)

you can iterate on map using <c:foreach>. goes on map#entryset(). each iteration gives map.entry object in turn has getkey() , getvalue() methods.

here's kickoff example:

<c:foreach items="${map}" var="entry"> key: ${entry.key}, value: ${entry.value}<br> </c:foreach>

java jsp join dao dto

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -