开发者

Spring-Hibernate: How to submit a for when the object has one-to-many relations?

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???


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜