Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Hi I am currently developing a web application in Spring and Hibernate where i have several different tables. The Delivery table for example is mapped many-to-one with the Customer table and the Product table.

I am wondering what the best practice is for creating a view function where I display all the columns related to each delivery but exclude the different IDs.

I can think of two ways of doing this:

  1. Create a DTO (data transfer object) with all the columns I wish to display and then in the controller I create a new instance of it and set the different values with setters and services in a for-loop for each Delivery ID. this is the method used in this example. Here is part of the controller code from it:

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String getRecords(Model model) {
     logger.debug("Received request to show records page");
    
     // Retrieve all persons
     List<Person> persons = personService.getAll();
    
     // Prepare model object
     List<PersonDTO> personsDTO = new ArrayList<PersonDTO>();
    
     for (Person person: persons) {
      // Create new data transfer object
      PersonDTO dto = new PersonDTO();
    
       dto.setId(person.getId());
       dto.setFirstName(person.getFirstName());
       dto.setLastName(person.getLastName());
       dto.setMoney(person.getMoney());
       dto.setCreditCards(creditCardService.getAll(person.getId()));
    
      // Add to model list
        personsDTO.add(dto);
        }
    
     // Add to model
     model.addAttribute("persons", personsDTO);
    
     // This will resolve to /WEB-INF/jsp/records.jsp
     return "records";
     } 
    
    1. the second way I am considering is to create an HQL query that Joins the relevant tables and then displaying that. I'm not sure exactly how to do this yet.

So which of the alternatives is the better way to go? Are there any others that I haven't thought about?

Thank you for the advice, D

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

With ORM technologies there are many ways to approach this. I think either of the ways you suggest are adequate, but I'll add the performance concern angle.

If you have performance concerns, then I suggest you look at the SQL produced (show_sql) as well as the network traffic via the JDBC driver (use P6Spy or similar) in both versions and see what impact they have.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.