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(); } }
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(); } }