Search⌘ K
AI Features

Develop the Business Service Tier

Explore the development of the business service tier focusing on many-to-many self-referencing relationships. Learn how to implement and test core operations such as finding all records, creating links, removing links, and checking the presence of relationships within Java Spring applications.

In this lesson, we will move to the business service tier.

“Find all records” operation

First, we will review the test to find all records.

Java
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath:context.xml" } )
@TransactionConfiguration( defaultRollback = true )
@Transactional
public class WorkerWorkerServiceImplTest {
@Autowired
private WorkerService workerService;
@Autowired
private WorkerWorkerService workerWorkerService;
@Test
public void testFindAll() {
Assert.assertEquals(0L, workerWorkerService.findAll().size());
}
}

Let’s examine how to program the related method in the business tier.

Java
@Service
@Transactional
public class WorkerWorkerServiceImpl implements WorkerWorkerService {
@Autowired
private WorkerDao workerDao;
@Autowired
private WorkerMapper workerMapper;
@Autowired
private WorkerWorkerDao workerWorkerDao;
@Override
public List<WorkerWorkerDto> findAll() {
List<WorkerWorkerDto> workerWorkerDtos = new ArrayList<WorkerWorkerDto>();
List<WorkerWorker> workerList = workerWorkerDao.getAll();
for(WorkerWorker workerWorker : workerList) {
WorkerWorkerDto workerWorkerDto = new WorkerWorkerDto();
workerWorkerDto.setWorkerId1(workerMapper.mapEntityToDto(workerWorker.getWorker1()));
workerWorkerDto.setWorkerId2(workerMapper.mapEntityToDto(workerWorker.getWorker2()));
workerWorkerDto.setRelationshipType(workerWorker.getRelationshipType());
workerWorkerDtos.add(workerWorkerDto);
}
return workerWorkerDtos;
}
}
...