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 Bank createBank() { Bank bank = new Bank(); bank.setName("First United Federal"); bank.setAddressLine1("103 Washington Plaza"); bank.setAddressLine2("Suite 332"); bank.setAddressType("PRIMARY"); bank.setCity("New York"); bank.setCreatedBy("Kevin Bowersox"); bank.setCreatedDate(new Date()); bank.setInternational(false); bank.setLastUpdatedBy("Kevin Bowersox"); bank.setLastUpdatedDate(new Date()); bank.setState("NY"); bank.setZipCode("10000"); return bank; }