Hibernate 和 Spring MVC 中从一个控制器访问两种服务方法

发布于 2024-12-09 07:08:31 字数 906 浏览 0 评论 0原文

我无法从一个控制器访问 2 种服务方法。我想创建一个任务,但同时我想创建新角色并将其自动增量 id 分配给任务的 roleId(外键)。这是一对一的映射。

我可以在 simpleFormContrller 中实现它吗?我该怎么做呢?

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(对象命令)抛出异常 {

    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;
}

}`

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 response to taskCreated view
    return mv;
}

}`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请叫√我孤独 2024-12-16 07:08:31

如果您在 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文