首页 > 解决方案 > 在JPA中,插入一个孩子并立即保存并删除该孩子不会删除数据库中的孩子

问题描述

我有一个 Parent 实体包含一个 Childs 列表。

结果:

有人可以给我一份文件来展示 JPA 如何不为 A2 添加父信息吗?

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Parent {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @Fetch(FetchMode.SELECT)
    @JoinColumn(name = "parent_id")
    List<Child> children;

    @Version
    private Long recordVersion;
}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Child {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;

    @ManyToOne
    @JoinColumn(name = "parent_id", insertable = false, updatable = false)
    private Parent parent;

    @Version
    private Long recordVersion;

    public String toString() {
        return "Child[id=" + id + ";name=" + name + "]";
    }
}
@Transactional
    public void addChild(Long parentId, String childName) {
        Parent parent = getParent(parentId);

        Child child = new Child();
        child.setName("A2");
        List<Child> children = Arrays.asList(child);
        parent.getChildren().addAll(children);
        parentRepository.save(parent);

        parent = getParent(parentId);
        parent.getChildren().clear();

        Child child1 = new Child();
        child1.setName("A3");
        List<Child> spaces1 = Arrays.asList(child1);

        parent.getChildren().addAll(spaces1);
        parentRepository.saveAndFlush(parent);
    }

更新 1:如果我注释掉 parentRepository.save(parent); 或者用 parentRepository.saveAndFlush(parent); 替换,数据库中只有 A3。

标签: jpaspring-data

解决方案


您面临的问题的最可能原因是双向关联未正确映射。事实上,您已经声明了两个完全不相关的关联,然后让它们共享连接列。

要正确映射双向一对多关联,您需要将“一”侧声明为相反侧,如下所示:

@OneToMany(mappedBy = "parent")
@Fetch(FetchMode.SELECT)
List<Child> children;

请注意,在这种情况下,Child是关联的拥有方。对 的更改Parent.children被完全忽略。

由于您似乎希望能够以Parent.children独占方式管理关联,因此您可能希望Parent通过删除属性来创建拥有方Child.parent(从而将关联更改为单向关联)。

请注意,以上两个选项是唯一有效的映射。您不能同时使关联的双方成为拥有方,并且在双向一对多关联中,“一”方不能成为拥有方。


推荐阅读