Develop the Business Service Tier
Let's see how the business service can be developed for a one-to-many bi-directional relationship scenario.
We'll cover the following...
We will now go to the business service tier.
Find all records operation
First, we will look at the test for the “fetch all records” operation.
Press + to interact
@RunWith( SpringJUnit4ClassRunner.class )@ContextConfiguration( locations = { "classpath:context.xml" } )@TransactionConfiguration( defaultRollback = true )@Transactionalpublic class ItemServiceImplTest {@Autowiredprivate ItemService service;@Testpublic void testFindAll(){Assert.assertEquals(0L, service.findAll().size());}}
Let’s see how to program the corresponding method in the business service.
Press + to interact
@Service@Transactionalpublic class ItemServiceImpl implements ItemService{@Autowiredprivate ItemDao dao;@Autowiredprivate ItemMapper mapper;@Overridepublic List<ItemDto> findAll() {List<Item> items = dao.getAll();List<ItemDto> itemDtos = new ArrayList<ItemDto>();if(null !=items){for (Item item : items) {itemDtos.add(mapper.mapEntityToDto(item));}}return itemDtos;}}
Mappers
As shown earlier, we have a mapper for translating data access objects into entities and vice-versa. It usually has two operations displayed underneath. One ...