0

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

asked May 24, 2012 at 11:15

1 Answer 1

1

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.

answered May 24, 2012 at 11:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.