Announcement Announcement Module
No announcement yet.
Save data from DTO and Service to table Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Save data from DTO and Service to table

    I have been trying to find an example that will help me but I haven't been able to. Hopefully someone here can help me out.

    In my application I have a DTO that I use for the view of my new-delivery page. Do I send the data to my DeliveryService through my controller: (not sure if this is right)

    public class DeliveryController {
      private DeliveryService deliveryService;
      @RequestMapping(value="/initForm", method=RequestMethod.GET)
      public String getAdd(Model model){
      DeliveryDto form = new DeliveryDto();
      model.addAttribute("formData", form);
      return "----view----";
      @RequestMapping(value="/postForm", method=RequestMethod.POST)
      public String postAdd(@ModelAttribute("formData") DeliveryDto form, Model model) {
      deliveryService.createDelivery(form.getCustomerName(), form.getCustomerCountry());
      return "----view----";
    Or do I retrieve that data in my DeliveryService?

    The reason I am using a DTO is that I have some IDs in my Delivery table that I don't want the user to have to enter.

    I use a customer Service+DTO to retrieve the ID corresponding to a pair of customerNames and customerCountries.


    public class CustomerDaoImpl implements CustomerDao {
    private SessionFactory sessionFactory;
    public Customer getCTID(String customerName, String customerCountry) { //Is it right to use a query here to get the Id?
        return (Customer) this.sessionFactory.getCurrentSession().createQuery(
                   "select customerId from Customer where customerName = :name and customerCountry = :country")
                   .setParameter("name", customerName).setParameter("country", customerCountry).uniqueResult();

    public class CustomerServiceImpl implements CustomerService {
    private CustomerDao customerDao;
    public Customer retrieveCTID(String customerName, String customerCountry) {
        return this.customerDao.getCTID(customerName, customerCountry);
    I don't know how I would write the DeliveryService/DAO so that it takes some DTO data and the data from CustomerService and persists it as a new delivery in the DeliveryTable.

    Thank you for your help!