Develop the Business Service Tier
Let's see how the business service can be developed for a many-to-many bi-directional relationship scenario.
We'll cover the following...
“Find all records” operation
First, we will check the test to find all the records.
Press + to interact
@RunWith( SpringJUnit4ClassRunner.class )@ContextConfiguration( locations = { "classpath:context.xml" } )@TransactionConfiguration( defaultRollback = true )@Transactionalpublic class ClientAccountServiceImplTest {@Autowiredprivate ClientService clientService;@Autowiredprivate AccountService accountService;@Autowiredprivate ClientAccountService clientAccountService;@Testpublic void testFindAll() {Assert.assertEquals(0L, clientAccountService.findAll().size());}}
Let’s review how to code this method in the business service.
Press + to interact
@Service@Transactionalpublic class ClientAccountServiceImpl implements ClientAccountService {@Autowiredprivate ClientDao clientDao;@Autowiredprivate ClientMapper clientMapper;@Autowiredprivate AccountDao accountDao;@Autowiredprivate AccountMapper accountMapper;@Autowiredprivate ClientAccountDao clientAccountDao;@Overridepublic List<ClientAccountDto> findAll() {List<ClientAccountDto> clientAccountDtos = new ArrayList<ClientAccountDto>();List<Client> clientList = clientAccountDao.getAll();for(Client client : clientList) {ClientDto clientDto = clientMapper.mapEntityToDto(client);Set<Account> accounts = client.getAccounts();for(Account account : accounts) {ClientAccountDto clientAccountDto = new ClientAccountDto();clientAccountDto.setClientDto(clientDto);AccountDto accountDto = accountMapper.mapEntityToDto(account);clientAccountDto.setAccountDto(accountDto);clientAccountDtos.add(clientAccountDto);}}return clientAccountDtos;}}
Create operation
Let’s move on to the create
test operation that creates one incidence of the ...