@Test public void testGetAllCategories(){ long a = categoryService.createCategory("a"); long b = categoryService.createCategory("b"); long c = categoryService.createCategory("c"); categoryService.createSubCategory(a, "1"); categoryService.createSubCategory(b, "2"); categoryService.createSubCategory(c, "3"); List<Category> categories = categoryService.getAllCategories(false); assertEquals(3, categories.size()); Category first = categories.get(0); assertThrows(Exception.class, () -> first.getSubCategories().size()); categories = categoryService.getAllCategories(true); Category readBack = categories.get(0); assertEquals(1, readBack.getSubCategories().size()); }
public List<Category> getAllCategories(boolean withSub){ TypedQuery<Category> query = em.createQuery("select c from Category c", Category.class); List<Category> categories = query.getResultList(); if(withSub){ categories.forEach(c -> c.getSubCategories().size()); } return categories; }
CategoryService { public List<Category> getAllCategories(boolean withSub){ TypedQuery<Category> query = em.createQuery("select c from Category c", Category.class); List<Category> categories = query.getResultList(); if(withSub){ categories.forEach(c -> c.getSubCategories().size()); } return categories; } }
CategoryService { public List<Category> getAllCategories(boolean withSub){ TypedQuery<Category> query = em.createQuery("select c from Category c", Category.class); List<Category> categories = query.getResultList(); if(withSub){ categories.forEach(c -> c.getSubCategories().size()); } return categories; }  }
CategoryService { public List<Category> getAllCategories(boolean withSub){ TypedQuery<Category> query = em.createQuery("select c from Category c", Category.class); List<Category> categories = query.getResultList(); if(withSub){ categories.forEach(c -> c.getSubCategories().size()); } return categories; }  Long createCategory(String name); Long createSubCategory(long parentId, String name); List<Category> getAllCategories(boolean withSub); Category getCategory(long id, boolean withSub); SubCategory getSubCategory(long id); }
CategoryService { public List<Category> getAllCategories(boolean withSub){ TypedQuery<Category> query = em.createQuery("select c from Category c", Category.class); List<Category> categories = query.getResultList(); if(withSub){ categories.forEach(c -> c.getSubCategories().size()); } return categories; }  Long createCategory(String name); Long createSubCategory(long parentId, String name); List<Category> getAllCategories(boolean withSub); Category getCategory(long id, boolean withSub); SubCategory getSubCategory(long id);  }