0

I have an entity called Order, which has an attribute that is a List of LineItem objects. When I persist an order, I would like the line item objects it contains to be persisted as well. How can I do this? I tried to set the cascadeType for the List to a cascade type of ALL, but that causes a constraint violation. I tried to then use MERGE, but that didn't add anything to the LineItem table. Any ideas?

public class Order {
    ...
    @OneToMany(mappedBy = "order")
    private List<LineItem> orderSegments;
    ...
}

public class LineItem {
    ...
    @ManyToOne
    @JoinColumn(name="ORDER_ID")
    private Order order;
    ...    
}

When I persist an Order, for some reason, nothing gets put into the line item table

user1154644
  • 4,491
  • 16
  • 59
  • 102
  • Could you share how you have done relationship mapping for Order and LineItem ? – SAP May 15 '14 at 00:42
  • You do need to use a cascade, what was the specific constraint violation that you received? – JamesENL May 15 '14 at 02:35
  • Well, I just re-ran my unit tests, and there is no violation, but it doesn't put anything in the line item table. I definitely am adding 2 line items to the order, then persisting the order. – user1154644 May 15 '14 at 02:39
  • Firstly, you need to add a Cascade option to the @OneToMany. Secondly you need to ensure both sides of the relationship are set. See here, for example: http://stackoverflow.com/a/23648953/1356423. That will fix your issue. – Alan Hay May 15 '14 at 11:06

0 Answers0