Search⌘ K

Develop the Business Service Tier

Explore how to develop the business service tier in a Java Spring and Hibernate application. Learn to implement key operations such as finding, creating, editing, and removing records with a focus on test-driven development and proper data mapping. This lesson prepares you to handle business logic and data manipulation efficiently before moving on to the presentation tier.

“Find all records” operation

We will start with the “find all records” operation.

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

Next, let’s learn how to code the corresponding “find all records” method in the business service.

Java
@Service
@Transactional
public class ProtocolServiceImpl implements ProtocolService {
@Autowired
private ProtocolDao protocolDao;
@Autowired
private ProtocolMapper protocolMapper;
@Override
public List<ProtocolDto> findAll() {
List<Protocol> protocols = protocolDao.getAll();
List<ProtocolDto> protocolDtos = new ArrayList<ProtocolDto>();
for(Protocol protocol : protocols){
protocolDtos.add(protocolMapper.mapEntityToDto(protocol));
}
return protocolDtos;
}
}

Mappers

The mapper typically has two operations that are used to convert data access objects to ...