コード例 #1
0
  public static void main(String[] args) {
    try {
      Context jndiContext = getInitialContext();
      Object ref = jndiContext.lookup("TravelAgentBean/remote");
      TravelAgentRemote dao = (TravelAgentRemote) ref;

      CustomerPK pk = new CustomerPK("Burke", 9999999);
      Customer cust = new Customer();
      cust.setPk(pk);
      cust.setFirstName("Bill");

      dao.createCustomer(cust);

      cust = dao.findCustomer("Burke", 9999999);
      System.out.println(cust.getFirstName());
      System.out.println(cust.getLastName());
      System.out.println(cust.getSsn());
    } catch (javax.naming.NamingException ne) {
      ne.printStackTrace();
    }
  }
コード例 #2
0
  public static Customer createCustomerAddress(EntityManagerFactory factory) {
    System.out.println("Create 1st Customer");
    Customer cust = new Customer();
    cust.setFirstName("Bill");
    cust.setLastName("Burke");
    Address address = new Address();
    address.setStreet("Beacon Street");
    address.setCity("Boston");
    address.setState("MA");
    address.setZip("02115");
    cust.setAddress(address);

    EntityManager manager = factory.createEntityManager();
    try {
      manager.getTransaction().begin();
      manager.persist(cust);
      manager.getTransaction().commit();
    } finally {
      manager.close();
    }
    System.out.println("Address was also persisted with auto-generated key: " + address.getId());
    System.out.println("Return detached Customer instance: " + cust.getId());
    return cust;
  }