Develop the Business Service Tier
Let's see how the business service can be developed for a one-to-one self-referencing relationship scenario.
We'll cover the following...
Let’s now jump to the business service tier.
Find all records operation
First, we will review the test for the “fetching all records” operation.
Press + to interact
@RunWith( SpringJUnit4ClassRunner.class )@ContextConfiguration( locations = { "classpath:context.xml" } )@TransactionConfiguration( defaultRollback = true )@Transactionalpublic class StudentServiceImplTest {@Autowiredprivate StudentService service ;@Testpublic void testFindAll() {Assert.assertEquals(0L, service.findAll().size());}}
Let’s see how to code the matching method in the business service. We retrieve all the records using the data access operation and return a list of StudentDto
using the mapper.
Press + to interact
@Service@Transactionalpublic class StudentServiceImpl implements StudentService{@Autowiredprivate StudentDao dao ;@Autowiredprivate StudentMapper mapper ;@Overridepublic List<StudentDto> findAll() {List<StudentDto> studentDTOs = new ArrayList<StudentDto>();List<Student> students = dao.getAll();if(null != students){for(Student student : students){studentDTOs.add(mapper.mapEntityToDto(student));}}return studentDTOs;}}
Mappers
As discussed earlier, we have a mapper that converts data access objects into entities and vice-versa. It ...