is it possible to let bi-directional fields autoupdate, if one side is changed?
class Customer {
@OneToManycascade=ALL, mappedBy="customer")
public list<Order> orders;
}
class Order {
@ManyToOne
private Customer customer;
}
If I set the customer field in class Order, do I also have to perform a add(order) to the orders list of the Customer class?
Or can the other side be updated automatically somehow?
ty
The way I would do it is to add the Order to the Customer and then persist the Customer which will automatically persist the “Owning side” of the Order.
Depending on the implementation you use too (JPA, Hibernate..etc), you may also need to add the customer to the order as well. But the cascading should take care of that in most implementations. I personally wouldn’t rely on determining what the implementation would do for you though and would add to both sides and persist them this way. I personally would also always store the non-owning side first. But again, depending on the implementation that also may not be neccessary.