Spring-Hibernate:当对象具有一对多关系时如何提交for?
我有一个表单更改了我的对象客户的属性。每个客户都有相关的订单。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是不从表单提交客户对象。
而是提交客户,仅提交客户 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.
嗨,
从订单侧设置多对一关系的cascade=“none”属性。
谢谢。
HI,
Make cascade="none" attribute of many-to-one relationship from order side.
Thanks.