Announcement Announcement Module
Collapse
No announcement yet.
Save data from DTO and Service to table Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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)

    Code:
    @Controller
    public class DeliveryController {
    
      @Resource(name="deliveryService")
      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.

    CustomerDAO:

    Code:
    @Repository
    public class CustomerDaoImpl implements CustomerDao {
    
    @Autowired(required=true)
    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();
    
    
        }
    }
    CustomerService:

    Code:
    @Service
    public class CustomerServiceImpl implements CustomerService {
    
    @Autowired(required=true)
    private CustomerDao customerDao;
    
    @Transactional
    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!

    /D
Working...
X