No need at all. The database design does not have to be consistent with the Java object design. Just include role in your user object, or the role object contains user.
Supplement:
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private UserRoleRepository userRoleRepository;
public void deleteUser(User user) {
userRepository.delete(user);
userRoleRepository.deleteRoles(user);
}
}
public class RoleService {
@Autowired
private RoleRepository roleRepository;
@Autowired
private UserRoleRepository userRoleRepository;
public void deleteRole(Role role) {
roleRepository.delete(role);
userRoleRepository.deleteUsers(role);
}
}
I would like to ask again, to obtain the role corresponding to the user, should it be placed in getUserRoles(User user), UserService or RoleService? What is the basis for judgment?
No need at all. The database design does not have to be consistent with the Java object design. Just include role in your user object, or the role object contains user.
Supplement:
I would like to ask again, to obtain the role corresponding to the user, should it be placed in
getUserRoles(User user)
,UserService
orRoleService
? What is the basis for judgment?It seems that it can be placed anywhere. If you want to avoid arguments, put it in a separate command