public static Customer createCreditCard(EntityManagerFactory factory, Customer cust) {
    CreditCard card = new CreditCard();
    card.setExpirationDate(new java.util.Date());
    card.setNumber("4444-4444-4444-4444");
    card.setNameOnCard("William Burke");
    card.setCreditOrganization("Capital One");
    card.setCustomer(cust);

    EntityManager manager = factory.createEntityManager();
    try {
      manager.getTransaction().begin();
      manager.persist(card);
      manager.getTransaction().commit();

      // Show that card.getCustomer() returns null

      manager.clear();

      CreditCard cardCopy = manager.find(CreditCard.class, card.getId());
      System.out.println("should be null: " + cardCopy.getCustomer());

      manager.getTransaction().begin();
      System.out.println("now set the owning side of the relationship");
      Customer custCopy = manager.find(Customer.class, cust.getId());
      custCopy.setCreditCard(cardCopy);
      manager.getTransaction().commit();

      manager.clear();

      cardCopy = manager.find(CreditCard.class, card.getId());
      System.out.println("should be set now: " + cardCopy.getCustomer().getFirstName());
    } finally {
      manager.close();
    }
    return cust;
  }