'JPA OneToMany and return child id

I have two entity - User and Task in OneToMany relaction. After add task to user i want to return from method an id of this task but it returns null (in database a task has a proper number id).

Method:

@RequiredArgsConstructor
class CreateTaskHandler {

private final UserRepository userRepository;
private final TaskContentValidator taskContentValidator;

@Transactional
Either<TaskError, TaskDto> handle(CreateTask command) {
   return userRepository.getByUsername(command.getUsername())
            .map(user -> taskContentValidator.validate(command.getContent())
                    .map(correctContent -> new Task(correctContent, command.getPriority(), command.getStatus(), user))
                    .map(task -> user.addTask(task))
                    .map(addedTask -> new TaskDto(addedTask.getId())))
            .getOrElseThrow(() -> new UserNotFoundException(command.getOwnerUsername()));
   }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source