title: JPA ManyToOne Relationship: foreign key not being stored in table of many side
body: <p>The subordinate side of OneToMany relationship:</p>
  
  <pre><code>@Entity
  @Table(name="xxx_customer")
  @XmlRootElement(name="Customer")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Customer implements Serializable {
  
     @OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
     private List&lt;CreditCard&gt; creditCards;
  
  
     public void addCreditCart(CreditCard creditCard) {
  
         creditCards.add(creditCard);
     }
  
  }
  </code></pre>
  
  <p>The owning side of ManyToOne relationship:</p>
  
  <pre><code>@Entity
  @Table(name="xxx_credit_card")
  @XmlRootElement(name="CreditCard")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class CreditCard implements Serializable {
  
      @ManyToOne(optional=false)
      @JoinColumn(name="customer_id", referencedColumnName="id")
      private Customer customer;
  }
  </code></pre>
  
  <p>Call from OrderManager to create Customer and CreditCard records:</p>
  
  <pre><code>@Stateless
  @TransactionManagement(TransactionManagementType.CONTAINER)
  public class OrderManager {
  
      @Inject
      @UserDatabase
      private EntityManager em;
  
      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      public void placeOrder(ShoppingCart cart) {
  
          try
          {
              Customer customer = cart.getCustomer();
              customer.addCreditCart(cart.getCreditCard());
              em.persist(customer);
          }
          catch(Exception ex)
          {
              ctx.setRollbackOnly();
  
              MessageUtil.addMessage(FacesMessage.SEVERITY_ERROR, 
                      "Place Order", "Error placing order!");
  
              ex.printStackTrace();
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>The data is created in both tables but the customer_id field of the CreditCard table is null. It does not contain the id of the Customer record as expected.</p>