Search⌘ K
AI Features

Develop the Business Service Tier

Explore how to implement the business service tier for many-to-many unidirectional relationships. Learn to develop and test CRUD operations such as find all, create, remove, and isPresent to manage User and Group links effectively in Java applications.

We will proceed with the business service tier.

“Find all records” operation

First, we will scan the test for the “find all records” operation.

Java
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath:context.xml" } )
@TransactionConfiguration( defaultRollback = true )
@Transactional
public class UserGroupServiceImplTest {
@Autowired
private UserService userService;
@Autowired
private GroupService groupService;
@Autowired
private UserGroupService userGroupService;
@Test
public void testFindAll() {
Assert.assertEquals(0L, userGroupService.findAll().size());
}
}

Let’s see how to code the equivalent method in the business service.

Java
@Service
@Transactional
public class UserGroupServiceImpl implements UserGroupService {
@Autowired
private GroupDao groupDao;
@Autowired
private UserDao userDao;
@Autowired
private UserGroupDao userGroupDao;
@Autowired
private UserMapper userMapper;
@Autowired
private GroupMapper groupMapper;
@Override
public List<UserGroupDto> findAll() {
List<UserGroupDto> userGroupDtos = new ArrayList<UserGroupDto>();
List<User> userList = userGroupDao.getAll();
for(User user : userList) {
UserDto userDto = userMapper.mapEntityToDto(user);
Set<Group> groups = user.getGroups();
for(Group group : groups) {
UserGroupDto userGroupDto = new UserGroupDto();
userGroupDto.setUserDto(userDto);
GroupDto groupDto = groupMapper.mapEntityToDto(group);
userGroupDto.setGroupDto(groupDto);
userGroupDtos.add(userGroupDto);
}
}
return userGroupDtos;
}
}

Create operation

Let’s move on to the create test ...