我从一个控制器访问2个服务方法。我想创建一个任务,但同时我想创建新的角色,并将其自动增量id分配给任务的roleId(外键)。它是一个一对一的映射。
我可以在simpleformcontroller中实现吗?我该怎么做呢?
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受保护的ModelAndView onSubmit(对象命令)抛出异常{
Task task = (Task) command;
//ContactService.createContact(contact);
hrService.createTask(task);
ModelAndView mv = new ModelAndView();
mv.setViewName(getSuccessView());
mv.addObject("taskCreated", task.getDescription()); // Pass response to taskCreated view
return mv;
}
} '
如果hibernate bean之间有正确的关系定义,应该没有问题。
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;
}