public static void main(String[] args) throws Exception {
    HashMap map = new HashMap();
    // map.put("hibernate.show_sql", "true");
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("titan", map);
    EntityManager entityManager = factory.createEntityManager();
    entityManager.getTransaction().begin();
    try {
      System.out.println("Initialize DB");
      InitializeDB.initialize(entityManager);
      System.out.println();
      System.out.println();

      System.out.println("Find Bill Burke by named parameter");
      Customer cust = findCustomerByNamedParameter(entityManager, "Bill", "Burke");
      System.out.println("Bill Burke's cust id: " + cust.getId());
      System.out.println();
      System.out.println();

      System.out.println("Find Gavin King by indexed parameter");
      cust = findCustomerByIndexedParameter(entityManager, "Gavin", "King");
      System.out.println("Gavin King's cust id: " + cust.getId());
      System.out.println();
      System.out.println();

      System.out.println("Output all customers via paging");
      List results;
      int first = 0;
      int max = 2;
      do {
        results = getCustomers(entityManager, max, first);
        Iterator it = results.iterator();
        while (it.hasNext()) {
          Customer c = (Customer) it.next();
          System.out.println(c.getFirstName() + " " + c.getLastName());
        }
        entityManager.clear();
        first = first + results.size();
      } while (results.size() > 0);
    } finally {
      entityManager.getTransaction().commit();
      entityManager.close();
      factory.close();
    }
  }
Пример #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;
  }
Пример #3
0
  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;
  }