【问题标题】:Hibernate deleting parent objects when trying to delete child尝试删除子对象时休眠删除父对象
【发布时间】:2021-07-23 15:44:03
【问题描述】:

我有自联接表 CATEGORY。当我尝试删除子条目时,我的父条目 ae 也会被删除。我正在使用 Oracle 19.3 Db。

例如。

[
    {
        "id": 5,
        "name": "parent",
        "display_name": "parent",
        "parent_id": 0
    },
    {
        "id": 6,
        "name": "child",
        "display_name": "child",
        "parent_id": 5
    }
]

删除 6 的条目后,ID 为 5 的条目也将被删除。 我的班级

@Entity
@Table(name="CATEGORY")
public class Category implements Serializable {

    @Id
    @Column(name = "id", nullable = false, updatable = false, unique = true)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "name", nullable = false, updatable = true, unique = false)
    private String name;

    @Column(name = "display_name", nullable = false, updatable = true, unique = false)
    private String displayName;

    @NotFound(action = NotFoundAction.IGNORE)
    @ManyToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="parent_id")
    private Category parent;

    //Setter and Getters and Constructors

Spring boot 的日志如下

2021-04-30 14:50:57.345 DEBUG 2588 --- [nio-8080-exec-3] org.hibernate.SQL                        : 
    delete 
    from
        category 
    where
        id=?
2021-04-30 14:50:57.345 TRACE 2588 --- [nio-8080-exec-3] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [4]
2021-04-30 14:50:57.403 DEBUG 2588 --- [nio-8080-exec-3] org.hibernate.SQL                        : 
    delete 
    from
        category 
    where
        id=?
2021-04-30 14:50:57.405 TRACE 2588 --- [nio-8080-exec-3] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [3]
2021-04-30 14:50:57.518  INFO 2588 --- [nio-8080-exec-3] c.e.c.service.impl.CategoryServiceImpl   : Course category deleted with chain

 

【问题讨论】:

    标签: java oracle spring-boot hibernate


    【解决方案1】:

    您可以尝试使用 hibernate 查看您的 @Cascade 注释。它在相关实体之间级联操作,看起来您正在级联所有操作。 cascade={CascadeType.ALL},其中都包括 CascadeType.REMOVE。 参考:https://www.educba.com/cascade-in-hibernate/

    【讨论】:

      猜你喜欢
      • 2013-05-25
      • 1970-01-01
      • 2011-04-12
      • 2021-05-22
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多