Question

I seem to keep tripping over this problem. And I can't seem to get a straight answer on the way to approach it.

I have an MVC web app using Java, Tomcat, JSP, MySQL, etc. I'm not using a framework. I have Model Entities representing each table in the database. I'm using DAOs to access the database.

Say you have a Person, a Job, and an Application.

In the View, you need to relate those together to produce a row for a listing.

So far, I've been doing JOINs in the DAO and returning a long SQL Result from the ResultSet using ResultSupport. Then I loop through it in the JSP using a JSTL forEach tag.

I understand this is not the best way to do it.

But then, what is it I need to create in the DAO to pass back each row of data? Do I define a new object with all the field names I need in the view? Is this a DTO? Or do I stuff the original objects in a HashMap and pass back that?

When I ask the database, I assume that doing a join is going to be more efficient than doing a bunch of small queries to return the individual objects.

There must be an accepted pattern for doing this? I would prefer to just follow an accepted solution.

Can someone please point me in the right direction?

Thanks

Was it helpful?

Solution

If you only have one single view, then creating a plain old java object (POJO) works fine. I would create an POJO which contains all the fields you need for a view. Then create a collection (E.G. ArrayList). The collection of POJO's is your Data Transfer Object (DTO).

The database connections are costly. So you want to open the database, get the data and store it your POJO collection. You then either close database connection or return to a pool (if you are using database connection pool). The point is, don't try to create an html display while holding on to a live database connection. Get the data, close the database connection (or return to a pool), then perform your display logic (I.E. the View).

Yes, you want to select all the information at once (do a database join). Same idea, use the database connection and then close it as quickly as possible. You want to avoid multiple open/close, open/close, etc.

Hopefully that helps. You might want to narrow down your question to something more specific. It's really difficult for folks to answer broad architectural questions . Good Luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top