...

/

Develop the Business Service Tier

Develop the Business Service Tier

Let's see how the business service can be developed for a many-to-many bi-directional relationship scenario.

“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 )
@Transactional
public class ClientAccountServiceImplTest {
@Autowired
private ClientService clientService;
@Autowired
private AccountService accountService;
@Autowired
private ClientAccountService clientAccountService;
@Test
public void testFindAll() {
Assert.assertEquals(0L, clientAccountService.findAll().size());
}
}

Let’s review how to code this method in the business service.

Press + to interact
@Service
@Transactional
public class ClientAccountServiceImpl implements ClientAccountService {
@Autowired
private ClientDao clientDao;
@Autowired
private ClientMapper clientMapper;
@Autowired
private AccountDao accountDao;
@Autowired
private AccountMapper accountMapper;
@Autowired
private ClientAccountDao clientAccountDao;
@Override
public 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 ...