title: JPA Mapping for order to line items
body: <p>I have declared an order entity. It is made up of a date, customer, and a list of LineItem objects.</p>
  
  <p>A Line Item has attributes of a product and quantity. I am trying to model this relationship with JPA. It seems incorrect for each LineItem to have an ID, but I'm not sure how to express the relationship between a LineItem and an Order in JPA:</p>
  
  <pre><code>@Entity
  @Table(name = "ORDERS")
  public class Order {
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO, generator = "cust_seq_gen")
     @SequenceGenerator(name = "cust_seq_gen", sequenceName = "CUSTOMER_SEQUENCE", allocationSize = 5)
     private int id;
     @Temporal(DATE)
     @Column(name = "DATE_PLACED")
     private Date datePlaced;
     @Enumerated(EnumType.STRING)
     @Column(name = "STATUS")
     private Status status;
     @JoinTable(
          name = "LINE_ITEM_JOIN",
          joinColumns = @JoinColumn(name = "ORDER_ID"),
          inverseJoinColumns = @JoinColumn(name = "LINE_ITEM_ID")
     )
     private List&lt;LineItem&gt; orderSegments;
     @Column(name = "TOTAL", precision = 10, scale = 2)
     private double total;
     @ManyToOne
     private Customer customer;
     @Column(name = "NOTES")
     private String notes;
     }
  </code></pre>
  
  <p>LineItem Class</p>
  
  <pre><code>@Entity
  @Table(name = "LINE_ITEMS")
  public class LineItem {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "line_item_seq_gen")
    @SequenceGenerator(name = "line_item_seq_gen", sequenceName = "LINE_ITEM_SEQUENCE",   allocationSize = 5)
    private Order associatedOrder;
    private Product product;
    private int quantity;
    ...
  }
  </code></pre>
  
  <p>I think that the primary key for a line item should be a combination of the product id, quantity, and order id, although I know I have not modeled that.</p>