Develop the Business Service Tier
Let's see how the business service can be developed for a many-to-many unidirectional relationship scenario
We'll cover the following...
We will proceed with the business service tier.
“Find all records” operation
First, we will scan the test for the “find all records” operation.
Press + to interact
@RunWith( SpringJUnit4ClassRunner.class )@ContextConfiguration( locations = { "classpath:context.xml" } )@TransactionConfiguration( defaultRollback = true )@Transactionalpublic class UserGroupServiceImplTest {@Autowiredprivate UserService userService;@Autowiredprivate GroupService groupService;@Autowiredprivate UserGroupService userGroupService;@Testpublic void testFindAll() {Assert.assertEquals(0L, userGroupService.findAll().size());}}
Let’s see how to code the equivalent method in the business service.
Press + to interact
@Service@Transactionalpublic class UserGroupServiceImpl implements UserGroupService {@Autowiredprivate GroupDao groupDao;@Autowiredprivate UserDao userDao;@Autowiredprivate UserGroupDao userGroupDao;@Autowiredprivate UserMapper userMapper;@Autowiredprivate GroupMapper groupMapper;@Overridepublic 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 ...