持久化内部实体,Spring Jpa

问题描述

如果实体内部有实体对象 接受内部实体对象的值,包括来自外部(控制器)的 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的内部实体会持续重复。 什么是一次性处理所有问题的好方法

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

提前感谢您的回答。

解决方法

只是内部对象级联类型默认

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...