Search⌘ K

Develop the Business Service Tier

Explore how to develop the business service tier by implementing core operations such as finding, creating, editing, and removing data records. Understand how to use mappers to convert between data access objects and entities, and learn to manage one-to-many self-referencing relationships within the service layer of Java Spring and Hibernate applications.

We will now proceed to the business service tier.

“Find all records” operation

First, we will look at the “find all records” operation.

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

Let’s learn how to package the routine in the business service.

Java
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService{
@Autowired
private CategoryDao dao;
@Autowired
private CategoryMapper mapper;
@Override
public List<CategoryDto> findAll() {
List<Category> categories = dao.getAll();
List<CategoryDto> categoryDtos = new ArrayList<CategoryDto>();
if(null !=categories && categories.size() > 0){
for(Category category : categories){
categoryDtos.add(mapper.mapEntityToDto(category));
}
}
return categoryDtos;
}
}

Mappers

As we said earlier, we have a mapper that converts data access objects into entities and vice-versa. This has two prominent procedures displayed below. ...