public static void main(String[] args) { EntityManagerFactory emf = null; EntityManager em = null; EntityTransaction tx = null; try { emf = Persistence.createEntityManagerFactory("oguz-finances"); em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); int pageNumber = 3; int pageSize = 2; // Bank bank = createBank(); // em.persist(bank); Bank bank = em.find(Bank.class, 1L); em.detach(bank); // System.out.println(em.contains(bank)); // bank.setName("Another Demonstration2"); Bank bank2 = em.merge(bank); // System.out.println(em.contains(bank2)); // System.out.println(bank2.getName()); bank.setName("doesnt occur"); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Transaction> criteriaQuery = cb.createQuery(Transaction.class); Root<Transaction> root = criteriaQuery.from(Transaction.class); Path<BigDecimal> amountPath = root.get("amount"); Path<String> transactionType = root.get("transactionType"); criteriaQuery .select(root) .where( cb.and( cb.le(amountPath, new BigDecimal("20.00")), cb.equal(transactionType, "Withdrawl"))); TypedQuery<Transaction> query = em.createQuery(criteriaQuery); // query.setFirstResult((pageNumber-1)*pageSize); // query.setMaxResults(pageSize); List<Transaction> transactions = query.getResultList(); for (Transaction t : transactions) { System.out.println(t.getTitle()); } tx.commit(); } catch (Exception e) { tx.rollback(); e.printStackTrace(); } finally { em.close(); emf.close(); } }
private static Transaction createShoePurchase(Account account) { Transaction shoePurchase = new Transaction(); shoePurchase.setAccount(account); shoePurchase.setTitle("Work Shoes"); shoePurchase.setAmount(new BigDecimal("100.00")); shoePurchase.setClosingBalance(new BigDecimal("0.00")); shoePurchase.setCreatedBy("Kevin Bowersox"); shoePurchase.setCreatedDate(new Date()); shoePurchase.setInitialBalance(new BigDecimal("0.00")); shoePurchase.setLastUpdatedBy("Kevin Bowersox"); shoePurchase.setLastUpdatedDate(new Date()); shoePurchase.setNotes("Nice Pair of Shoes"); shoePurchase.setTransactionType("Debit"); return shoePurchase; }
private static Transaction createNewBeltPurchase(Account account) { Transaction beltPurchase = new Transaction(); beltPurchase.setAccount(account); beltPurchase.setTitle("Dress Belt"); beltPurchase.setAmount(new BigDecimal("50.00")); beltPurchase.setClosingBalance(new BigDecimal("0.00")); beltPurchase.setCreatedBy("Kevin Bowersox"); beltPurchase.setCreatedDate(new Date()); beltPurchase.setInitialBalance(new BigDecimal("0.00")); beltPurchase.setLastUpdatedBy("Kevin Bowersox"); beltPurchase.setLastUpdatedDate(new Date()); beltPurchase.setNotes("New Dress Belt"); beltPurchase.setTransactionType("Debit"); return beltPurchase; }