java—在hibernate中更新时,如果父实体中不包含子实体,则取消子实体的链接

qgelzfjb  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(201)

我在父母和孩子之间有一种双向的单亲关系。
问题是,每次我从父类调用update类时,即使旧子类(即创建时包含的子类)不是updatejson的一部分,子类也不会“取消链接”(即将parent\u id设置为null)。事实上,我可以从日志中看到,旧的child是“未链接”的,但数据库保持不变。请注意,我不想删除子项,我只想将其父项id设置为null。
请在下面找到我的DAO和控制器:
parentdao.java文件

@Entity
public class Parent implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @CreationTimestamp
    private Timestamp created;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER,
            mappedBy = "parent")
    private Set<Child> children;

    \\getters, setters

     public void removeFromParent(Set<Child> children){
        for(Child child: children) {
            cttpEmulationModel.setParent(null);
        }
    }

    @PrePersist
    public void addPrePersists() {
        if (children != null) {
            children.forEach(c -> c.setParent(this));
        }

    }

    @PreUpdate
    public void updatePrePersists() {
        if (children != null) {
            children.forEach(c -> c.setParent(this));
        }
    }

   @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Parent that = (Parent) o;
        return name.equals(that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

childdao.java文件

@Entity
public class Child implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @CreationTimestamp
    private Timestamp created;
    @ManyToOne
    @JoinColumn(name="p_id",referencedColumnName="id")
    private Parent parent;
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "att_id", referencedColumnName = "id)
    private Attribute attribte;

    public Long getId() {
        return getId;
    }

    public void setId(Long getId) {
        this.getId = getId;
    }

    @JsonIgnore
    public Parent getParent() {
        return parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;
    }
    p
    ublic Attribute getAttribute() {
        return attribute;
    }

    public void setAttribute(Attribute attribute) {
        this.attribute = attribute;
    }

    public Timestamp getCreated() {
        return created;
    }

    public void setCreated(Timestamp created) {
        this.created = created;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Child that = (Child) o;
        return attribute.equals(that.attribute);
    }

    @Override
    public int hashCode() {
        return Objects.hash(attribute);
    }
}

restcontroller.java文件

@PutMapping("")
    public ResponseEntity<?> updateParent(@RequestBody @Valid Parent parent) {
        Parent oParent = parentService.findByID(parent.getId());
        if (oParent == null) {
            return new ResponseEntity<String>("Parent is null", HttpStatus.BAD_REQUEST);
        }
        try {
            parent.removeFromTrainingScenario(oParent.getChildren());
            parentService.update(parent);
        } catch (Exception e) {
            return new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        ...
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题