@Test @Transactional public void testInitialize() { Category category = categoryDao.get(1L); categoryDao.initialize(category); Set<Book> books = category.getBooks(); }
@Test @Transactional public void testKeys() { Page<Book> page = new Page<Book>(); List<Serializable> keys = bookDao.getKeys(page); assertEquals(10, keys.size()); }
@Test @Transactional public void testCopy() { EntityManager em = bookDao.getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> criteria = cb.createQuery(Book.class); // Fetch join Root<Book> root = criteria.from(Book.class); Path<String> path = root.join("author").<String>get("name"); root.fetch("author"); criteria.select(root); // SubQuery Subquery<String> sq = criteria.subquery(String.class); Root<Author> author = sq.from(Author.class); sq.select(author.<String>get("name")); sq.where(cb.equal(author.<String>get("name"), "Rod")); criteria.where(cb.in(path).value(sq)); CriteriaQuery<Book> copy = cb.createQuery(Book.class); JpaUtils.copyCriteria(criteria, copy); List<Book> copyBooks = em.createQuery(copy).getResultList(); List<Book> books = em.createQuery(criteria).getResultList(); assertEquals(books, copyBooks); }
@Test @Transactional public void testCountQuery() { Filter f = new BeanFilter("joinFilter"); Page<Book> p = new Page<Book>(); p.setFilter(f); bookDao.getPage(p); assertEquals(22, p.getCount()); }
@Test @Transactional public void testFilter() { BookFilter bf = new BookFilter("bookFilter"); bf.setCategory(new Category("Java")); Page<Book> page = new Page<Book>(10); page.setFilter(bf); bookDao.getPage(page); List<Book> books = page.getData(); assertEquals(10, books.size()); }
@Test @Transactional public void testNamedQuery() { BookFilter filter = new BookFilter("booksByAuthorName"); filter.setAuthorName("Martin"); Page<Book> page = new Page<Book>(); page.setFilter(filter); page.setSortName("publishedDate"); page.setOrderDesc(); bookDao.getPage(page); assertEquals(4, page.getData().size()); }
@Test @Transactional public void testCountCriteria() { EntityManager em = bookDao.getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> criteria = cb.createQuery(Book.class); Root<Book> root = criteria.from(Book.class); Join<Book, Author> join = root.join("author"); criteria.where(join.isNotNull()); CriteriaQuery<Long> countCriteria = JpaUtils.countCriteria(em, criteria); Long result = em.createQuery(countCriteria).getSingleResult(); log.debug("Count: " + result); }