Search⌘ K
AI Features

Develop the Business Service Tier

Explore the development of the business service tier focusing on one-to-one self-referencing relationships. This lesson helps you understand how to implement, test, and manage core operations such as fetching, creating, editing, and removing student entities along with their mentors, using mappers and data access layers within Java Spring and Hibernate environments.

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.

Java
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath:context.xml" } )
@TransactionConfiguration( defaultRollback = true )
@Transactional
public class StudentServiceImplTest {
@Autowired
private StudentService service ;
@Test
public 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.

Java
@Service
@Transactional
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentDao dao ;
@Autowired
private StudentMapper mapper ;
@Override
public 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 typically has two operations, mapDtoToEntity and mapEntityToDto ...