@PersistenceContext (unitName="RAD75JPA",type=PersistenceContextType.TRANSACTION) private EntityManager entityMgr; public void addCustomer(Customer customer) throws ITSOBankException { System.out.println("addCustomer: " + customer.getSsn()); entityMgr.persist(customer); } public void closeAccount(String ssn, String id) throws ITSOBankException { System.out.println("closeAccount: " + id + " of customer " + ssn); Customer customer = getCustomer(ssn); Account account = getAccount(id); Transaction[] trans = getTransactions(id); for (Transaction tx : trans) { entityMgr.remove(tx); } entityMgr.remove(account); System.out.println("closed account with " + trans.length + " transactions"); } public void deleteCustomer(String ssn) throws ITSOBankException { System.out.println("deleteCustomer: " + ssn); Customer customer = getCustomer(ssn); Account[] accounts = getAccounts(ssn); for (Account acct : accounts) { closeAccount(ssn, acct.getId()); } entityMgr.remove(customer); } public void deposit(String id, BigDecimal amount) throws ITSOBankException { System.out.println("deposit: " + id + " amount " + amount); Account account = getAccount(id); try { Transaction tx = account.processTransaction(amount, Transaction.CREDIT); entityMgr.persist(tx); } catch (Exception e) { throw new ITSOBankException(e.getMessage()); } } public Account getAccount(String id) throws ITSOBankException { System.out.println("getAccount: " + id); try { return entityMgr.find(Account.class, id); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); throw new ITSOBankException(id); } } public Account[] getAccounts(String ssn) throws ITSOBankException { System.out.println("getAccounts: " + ssn); Query query = null; try { query = entityMgr.createNamedQuery("getAccountsBySSN"); query.setParameter(1, ssn); ListaccountList = query.getResultList(); Account[] array = new Account[accountList.size()]; return accountList.toArray(array); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); throw new ITSOBankException(ssn); } } public Customer getCustomer(String ssn) throws ITSOBankException { System.out.println("getCustomer: " + ssn); //Query query = null; try { //query = entityMgr.createNamedQuery("getCustomerBySSN"); //query.setParameter(1, ssn); //return (Customer)query.getSingleResult(); return entityMgr.find(Customer.class, ssn); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); throw new ITSOBankException(ssn); } } public Customer[] getCustomers(String partialName) throws ITSOBankException { System.out.println("getCustomer: " + partialName); Query query = null; try { query = entityMgr.createNamedQuery("getCustomersByPartialName"); query.setParameter(1, partialName); List beanlist = query.getResultList(); Customer[] array = new Customer[beanlist.size()]; return beanlist.toArray(array); } catch (Exception e) { throw new ITSOBankException(partialName); } } public Transaction[] getTransactions(String accountID) throws ITSOBankException { System.out.println("getTransactions: " + accountID); Query query = null; try { query = entityMgr.createNamedQuery("getTransactionsByID"); query.setParameter(1, accountID); List transactionsList = query.getResultList(); Transaction[] array = new Transaction[transactionsList.size()]; return transactionsList.toArray(array); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); throw new ITSOBankException(accountID); } } public String openAccount(String ssn) throws ITSOBankException { System.out.println("openAccount: " + ssn); Customer customer = getCustomer(ssn); int acctNumber = (new java.util.Random()).nextInt(899999) + 100000; String id = "00" + ssn.substring(0, 1) + "-" + acctNumber; Account account = new Account(); account.setId(id); entityMgr.persist(account); //customer.getAccountCollection().add(account); // does not work java.util.Set custSet = new java.util.TreeSet(); custSet.add(customer); account.setCustomerCollection(custSet); System.out.println("openAccount: " + id); return id; } public void transfer(String idDebit, String idCredit, BigDecimal amount) throws ITSOBankException { System.out.println("transfer: " + idCredit + " " + idDebit + " amount " + amount); withdraw(idDebit, amount); deposit(idCredit, amount); } public void updateCustomer(String ssn, String title, String firstName, String lastName) throws ITSOBankException { System.out.println("updateCustomer: " + ssn); Customer customer = getCustomer(ssn); customer.setTitle(title); customer.setLastName(lastName); customer.setFirstName(firstName); System.out.println("updateCustomer: " + customer.getTitle() + " " + customer.getFirstName() + " " + customer.getLastName()); } public void withdraw(String id, BigDecimal amount) throws ITSOBankException { System.out.println("withdraw: " + id + " amount " + amount); Account account = getAccount(id); try { Transaction tx = account.processTransaction(amount, Transaction.DEBIT); entityMgr.persist(tx); } catch (Exception e) { throw new ITSOBankException(e.getMessage()); } }