I'm trying to write an EJB3 application that has a one to many relationship. Unfortunately, there is no surrogate id on the many side. The schema looks like this:
Table ORDERS has PK ORDERID
Table ORDERLINES as FK ORDERID and local id ORDERLINEID. However, ORDERLINEID is not a key for the table. It's just the line number in the order. (line 1, line 2, etc..)
This is easy enough to map. The OrderLine object has an @EmbeddedId representing the composite ORDERID/ORDERLINEID. This maps fine.
The OrderLine object also can map the Order relation, but I have to mark it as not updatable:
@ManyToOne @JoinColumn(name="ORDERID",updatable=false,insertable=false) public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; }
Order order = new Order(); // fill in some field em.persist(order); // make sure we get the id // ... for (OrderLine line: lines) { // fill in line details line.getPk().setOrderId(order.getOrderId()); line.setOrder(order); }
Enable cascade PERSIST, it should work