@Test public void testCreateSubCategory(){ String ctgName = "ctg_testCreateSubCategory"; long ctgId = ctgEjb.createCategory(ctgName); String subName = "sub_testCreateSubCategory"; long subId = ctgEjb.createSubCategory(ctgId, subName); SubCategory sub = ctgEjb.getSubCategory(subId); assertEquals((Long) ctgId, sub.getParent().getId()); assertEquals(subName, sub.getName()); }
public Long createSubCategory(long parentId, String name){ Category category = em.find(Category.class, parentId); if(category == null){ throw new IllegalArgumentException("Category with id "+parentId+" does not exist"); } SubCategory subCategory = new SubCategory(); subCategory.setName(name); subCategory.setParent(category); em.persist(subCategory); category.getSubCategories().add(subCategory); return subCategory.getId(); }
CategoryEjb { public Long createSubCategory(long parentId, String name){ Category category = em.find(Category.class, parentId); if(category == null){ throw new IllegalArgumentException("Category with id "+parentId+" does not exist"); } SubCategory subCategory = new SubCategory(); subCategory.setName(name); subCategory.setParent(category); em.persist(subCategory); category.getSubCategories().add(subCategory); return subCategory.getId(); } }
CategoryEjb { public Long createSubCategory(long parentId, String name){ Category category = em.find(Category.class, parentId); if(category == null){ throw new IllegalArgumentException("Category with id "+parentId+" does not exist"); } SubCategory subCategory = new SubCategory(); subCategory.setName(name); subCategory.setParent(category); em.persist(subCategory); category.getSubCategories().add(subCategory); return subCategory.getId(); }  }
CategoryEjb { public Long createSubCategory(long parentId, String name){ Category category = em.find(Category.class, parentId); if(category == null){ throw new IllegalArgumentException("Category with id "+parentId+" does not exist"); } SubCategory subCategory = new SubCategory(); subCategory.setName(name); subCategory.setParent(category); em.persist(subCategory); category.getSubCategories().add(subCategory); return subCategory.getId(); }  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); }
CategoryEjb { public Long createSubCategory(long parentId, String name){ Category category = em.find(Category.class, parentId); if(category == null){ throw new IllegalArgumentException("Category with id "+parentId+" does not exist"); } SubCategory subCategory = new SubCategory(); subCategory.setName(name); subCategory.setParent(category); em.persist(subCategory); category.getSubCategories().add(subCategory); return subCategory.getId(); }  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);  }