Search⌘ K

Develop the Business Service Tier

Explore how to develop the business service tier handling one-to-many bi-directional relationships in Java using Spring. Understand how to implement and test methods for fetching, creating, editing, and removing records, ensuring smooth data management and integration with the data access layer.

We will now go to the business service tier.

Find all records operation

First, we will look at the test for the “fetch all records” operation.

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

Let’s see how to program the corresponding method in the business service.

Java
@Service
@Transactional
public class ItemServiceImpl implements ItemService{
@Autowired
private ItemDao dao;
@Autowired
private ItemMapper mapper;
@Override
public List<ItemDto> findAll() {
List<Item> items = dao.getAll();
List<ItemDto> itemDtos = new ArrayList<ItemDto>();
if(null !=items){
for (Item item : items) {
itemDtos.add(mapper.mapEntityToDto(item));
}
}
return itemDtos;
}
}

Mappers

As shown earlier, we have a mapper for translating data access objects into entities and vice-versa. It usually has two operations displayed underneath. One thing to note ...