title: Mapping of Product items to an order using a Map<Product, Integer>
body: <p>I have 4 tables:</p>
  <ul>
  <li>Stock (StockID, AvailableQty)</li>
  <li>Customers(CustomerID, Name)</li>
  <li>Orders (OrderID, CustomerID)</li>
  <li>OrderDetails (OrderDetailsID, OrderID, StockID, Quantity)</li>
  </ul>
  <p>A customer can have many orders, and an order consists of many orderDetails items.
  I'm trying to store the stock items in map that consists of the StockItem and a Integer being the quantity.</p>
  <pre><code>@Entity
  @Table(name = &quot;Orders&quot;)
  @AttributeOverride(name = &quot;id&quot;, column = @Column(name = &quot;OrderID&quot;))
  public class OrderEntity extends IdentifiableEntity {
  
      @OneToOne //Not sure if it is one to one?
      @JoinColumn(name = &quot;CustomerID&quot;)
      private CustomerEntity customer;
  
      @ElementCollection
      @CollectionTable(name = &quot;OrderDetails&quot;, joinColumns = @JoinColumn(name = &quot;StockID&quot;))
      @Column(name = &quot;Quantity&quot;)
      @MapKeyJoinColumn(name = &quot;OrderID&quot;)
      private Map&lt;StockEntity, Integer&gt; orderItems;
  
  }
  </code></pre>
  <p>When I try to persist it this way, the first stock item is added to the the OrderDetails table with the orderID of 1. But the next one is inserted using the orderID of 2 (which doesn't exist).</p>
  <p>Any help?</p>