【问题标题】:Persisting inner entities, Spring Jpa持久化内部实体,Spring Jpa
【发布时间】:2021-05-06 04:55:13
【问题描述】:

如果实体内部有实体对象 接受内部实体对象的值,包括来自外部(控制器)的 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 的内部实体不断重复。 有什么好办法一次性搞定?

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

感谢您提前回答。

【问题讨论】:

    标签: java spring jpa persistence


    【解决方案1】:

    只是内部对象 cascadeType 默认

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 1970-01-01
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      相关资源
      最近更新 更多