Spring-Hibernate:当对象具有一对多关系时如何提交for?

发布于 2024-10-11 10:58:43 字数 1005 浏览 4 评论 0原文

我有一个表单更改了我的对象客户的属性。每个客户都有相关的订单。 ORDER 表有一列 customer_id 用于映射。到目前为止一切正常,我可以毫无问题地阅读客户。

例如,当我现在更改表单中的 CUSTOMER 名称(不显示订单)时,保存后名称会更新,但 ORDERS 表中的所有关系都设置为 NULL(项目的 customer_id 设置为 NULL)我

怎样才能保持关系正常运作?

THX


更新:映射信息

订单映射到客户端

@OneToMany
@JoinColumn(name = "customer_id")
@OrderBy("orderDate")
private Collection<Order> orders = new LinkedList<Order>();

更新。 似乎

@SessionAttributes("customer")

在我的模型中添加 a ,将方法更改为

public String saveTrip(@ModelAttribute("customer") Customer customer, BindingResult result, SessionStatus status) {

    if (!result.hasErrors()) {          
        this.tripManager.saveTrip(trip);
    }
    else {
        logger.debug("Form data included errors, did not save data");
        BindingUtils.logBindingErrors(result, logger);
    }

    status.setComplete();

    return "redirect:/customers/";
}

可以解决问题。但这是解决问题的好方法吗?

I have a form changeed the properties of my object CUSTOMER. Each customer has related ORDERS. The ORDER's table has a column customer_id which is used for the mapping. All works so far, I can read customers without any problem.

When I now e.g. change the name of the CUSTOMER in the form (which does NOT show the orders), after saving the name is updated, but all relations in the ORDERS table are set to NULL (the customer_id for the items is set to NULL.

How can I keep the relationship working?

THX


UPDATE: Mapping Info

The Orders are mapped on the Customer side

@OneToMany
@JoinColumn(name = "customer_id")
@OrderBy("orderDate")
private Collection<Order> orders = new LinkedList<Order>();

UPDATE
Seems like adding a

@SessionAttributes("customer")

to my model, changing the method to

public String saveTrip(@ModelAttribute("customer") Customer customer, BindingResult result, SessionStatus status) {

    if (!result.hasErrors()) {          
        this.tripManager.saveTrip(trip);
    }
    else {
        logger.debug("Form data included errors, did not save data");
        BindingUtils.logBindingErrors(result, logger);
    }

    status.setComplete();

    return "redirect:/customers/";
}

Could solve the issu. But is this a good way of solving it???

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

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

发布评论

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

评论(2

墨落成白 2024-10-18 10:58:44

一种方法是不从表单提交客户对象。

而是提交客户,仅提交客户 ID 和新名称。在控制器中,您必须通过提交的 ID 加载客户,然后更新名称。并再次坚持客户。

One way would be not to submit the CUSTOMER Object from the form.

Instead submit the customer, submit only the customers ID and the new Name. In the controller you have to load the Customer by the submitted ID and then update the Name. And persist the Customer again.

一紙繁鸢 2024-10-18 10:58:44

嗨,
从订单侧设置多对一关系的cascade=“none”属性。
谢谢。

HI,
Make cascade="none" attribute of many-to-one relationship from order side.
Thanks.

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