1

I am currently playing around with a Spring MVC application, with two tables.

Student (id, name) and Publications (student_id, journal).

In my GET request, I am trying to request all of the Journals (String) of a given student ID. I believe in this case, I can simply do the following in my DAO without using any models:

@Override
public List<String> getAllJournals(String name) {
    String query = "SELECT journal FROM Publications INNER JOIN Student ON "
            + "Publications.student_id = Student.id WHERE Student.name=:name";

    Map<String,Object> params = new HashMap<String,Object>();
    params.put("name", name);
    List<String> journals = jdbcTemplate.queryForList(query, params, String.class);
    return journals;
}

Should I be using the POJO for the Publication class or the Student class? Wouldn't this be redundant?

hysoftwareeng
  • 497
  • 4
  • 15

1 Answers1

0

It's a good idea to have POJOs, since they allow you to use their attributes instead of making an attribution for each attribute you want to use, just like you did here:

List<String> journals = jdbcTemplate.queryForList(query, params, String.class);

Even if it seems a kind of "redundant" thing, using POJOs helps you to have reuse aspect on you code. You can simply create methids that returns all the Publication attributes. In your case, this is a very simple entity with just 2 fields, but if this entity grows up, use POJOs could help you with the reuse aspect.

Here is a great aswer about advantages of using POJOs.

andreybleme
  • 689
  • 9
  • 23
  • In this if I don't care about the student_ids at all, would it be better to just call the setter on Publications, like `publication.set(journals); return publications;` – hysoftwareeng Jul 22 '17 at 17:29
  • Yes, agreed. If you don't care about reusing object attributes, you can just make a query per result needed. – andreybleme Jul 22 '17 at 17:55