Access two services methods from one controller in hibernate and Spring MVC
I am stuck with access 2 services methods from one controller. I want to create a Task but same time I want to create new Role and assign its auto increment id to Task's roleId(foreign key). It is a one to one mapping.
Can I implement that in simpleFormContrller? And how can I do it?
public class CreateTaskController extends SimpleFormController {
HResourceService hrService; // Create a Service class instance.
public void setHResourceService(HResourceService hrservice) {
this.hrService = hrservice;
}
public CreateTaskController() {
setCommandClass(Task.class);
`
` @Override protected ModelAndView onSubmit(Object command) throws Exception {
Task task = (Task) command;
//ContactService.createContact(contact);
hrService.createTask(task);
ModelAndView mv = new ModelAndView();
mv.setViewName(getSuccessView());
mv.addObject("taskCreated", task.getDescription()); // Pass respons开发者_运维问答e to taskCreated view
return mv;
}
}`
There should be no problem if you have correct relation definition between hibernate beans.
public class Role implements Serializable {
@OneToOne(cascade=CascadeType.ALL,mappedBy="task")
private Task task;
}
public class Task implements Serializable {
....
@OneToOne
@JoinColumn(cascade=CascadeType.ALL,"role_id")
private Role role;
....
}
@Override protected ModelAndView onSubmit(Object command) throws Exception {
Task task = (Task) command;
task.setRole(new Role("big_brother").setTask(task));
hrService.createTask(task);
ModelAndView mv = new ModelAndView();
mv.setViewName(getSuccessView());
mv.addObject("taskCreated", task.getDescription()); // Pass response to taskCreated view
return mv;
}
精彩评论