Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Save Data From Both Dto And Services As A New Entry In A Table

hibernate spring dto database

  • Please log in to reply
No replies to this topic

#1 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 19 April 2012 - 03:05 AM

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)
@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:
@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:
@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
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download