java持久化内部实体,SpringJPA

fhg3lkii  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(368)

如果实体内部有实体对象,则在授予持久性之后接受内部实体对象的值(包括来自外部(控制器)的id),保存外部实体。

public class IssueCommentService {
    public IssueComment toEntity(Long id){
        return repository.findById(id).orElseThrow(NoContentFromRequestException::new);
    }

    public IssueComment toEntity(IssueComment notPersistIssueComment){
        if (Objects.isNull(notPersistIssueComment.getId())) {
            throw new CanNotBecomeEntityException();
        }
        return toEntity(notPersistIssueComment.getId());
    }
}

public class IssueCommentController {

    @PatchMapping(value = "")
    public ResponseEntity<IssueComment> updateCommentIssueComment(@RequestBody IssueComment issueComment) {
        String updateComment = issueComment.getComment();
        IssueComment entityIssueComment = issueCommentService.toEntity(issueComment);
        issueCommentService.updateComment(entityIssueComment, updateComment);
        return new ResponseEntity<>(issueCommentService.toEntity(entityIssueComment), HttpStatus.OK);
    }

}

此时,包含id的内部实体被持续地重复。什么是一次处理这一切的好方法?
内部对象应该每次都是持久的吗?
提前谢谢你的回答。

相关问题