Develop the Business Service Tier
Let's see how the business service can be developed for a one-to-many self-referencing relationship scenario
We'll cover the following...
We will now proceed to the business service tier.
“Find all records” operation
First, we will look at the “find all records” operation.
Press + to interact
@RunWith( SpringJUnit4ClassRunner.class )@ContextConfiguration( locations = { "classpath:context.xml" } )@TransactionConfiguration( defaultRollback = true )@Transactionalpublic class CategoryServiceImplTest {@Autowiredprivate CategoryService service;@Testpublic void testFindAll() {Assert.assertEquals(0L, service.findAll().size());}}
Let’s learn how to package the routine in the business service.
Press + to interact
@Service@Transactionalpublic class CategoryServiceImpl implements CategoryService{@Autowiredprivate CategoryDao dao;@Autowiredprivate CategoryMapper mapper;@Overridepublic List<CategoryDto> findAll() {List<Category> categories = dao.getAll();List<CategoryDto> categoryDtos = new ArrayList<CategoryDto>();if(null !=categories && categories.size() > 0){for(Category category : categories){categoryDtos.add(mapper.mapEntityToDto(category));}}return categoryDtos;}}
Mappers
As we said earlier, we have a mapper that converts data access objects into entities and vice-versa. This has two prominent procedures displayed below. ...