I have two models, with a third model to represent the relationship (can't just use @ManyToMany as the relationship has attributes). The primary key on the relationship model is a composite primary key of two other models. When I add a new relationship model to the @OneToMany set on one of the first models, can I get the relationship model's key to change automatically, or do I have to do it manually?
e.g. with three models like (psuedo-Java-Scala coming up)
class Order(val customerName)
@OneToMany
Set<OrderItem> items;
class Item(itemDescription)
@OneToMany
Set<OrderItem> orders;
class OrderItem(val quantity)
@Id
@PrimaryJoinColumn
@ManyToOne
Order order;
@Id
@PrimaryJoinColumn
@ManyToOne
Item item;
OrderItem oi;
oi.item = bananas;
order.items.add(oi);
order.save(); // This saves oi, but with a null value for order_id
oi.order = order; //This seems redundant, but the save is wrong without it.
It seems like I have to do it manually, but to my mind adding it to the Set should be enough information to change the keys. Is my thinking wrong or am I using JPA wrong?