繁体   English   中英

持久化内部实体,Spring Jpa

[英]Persisting inner entities, Spring Jpa

如果实体内部有实体 object 接受内部实体 object 的值,包括来自外部(控制器)的 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 的内部实体会不断重复。 有什么好的方法可以一次性处理?

内部 object 是否应该每次都持久化?

感谢您提前回答。

只是内部 object cascadeType 默认

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM