删除中间实体

时间:2017-06-07 09:52:30

标签: java hibernate

我有3个实体:爷爷,父亲和孩子,有着预期的关系 爷爷有很多父亲和父亲有很多孩子

@Entity
@Table(name = "Grandpa")
public class Grandpa {
     @Id
     @GeneratedValue(generator = "increment")
     @GenericGenerator(name = "increment", strategy = "increment")
     @Column(name = "id")
     private int id;

     @CreationTimestamp
     @Temporal(TemporalType.TIMESTAMP)
     @Column(name = "created_at")
     private Date createdAt;

     @UpdateTimestamp
     @Temporal(TemporalType.TIMESTAMP)
     @Column(name = "updated_at")
     private Date updatedAt;

     @OneToMany(cascade = {CascadeType.ALL}, mappedBy = "grandpa")
     private Set<Father> children = new HashSet<Father>(0);

     ....
}


@Entity
@Table(name = "Father")
public class Father {
    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    @Column(name = "id")
    private int id;

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at")
    private Date createdAt;

    @UpdateTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated_at")
    private Date updatedAt;

    @OneToOne(cascade = {CascadeType.ALL})
    private Grandpa grandpa;

    @OneToMany(cascade = {CascadeType.ALL}, mappedBy = "father")
    private Set<Child> children = new HashSet<Child>(0);

    ....
}



@Entity
@Table(name = "Child")
public class Child {
    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    @Column(name = "id")
    private int id;

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at")
    private Date createdAt;

    @UpdateTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated_at")
    private Date updatedAt;

    @OneToOne(cascade = {CascadeType.ALL})
    private Father father;
}

当我删除爷爷时,他所有的父亲和孩子也被删除了,但当我删除父亲时,他的孩子留在数据库中

我错过了什么?

1 个答案:

答案 0 :(得分:0)

发现问题......

实施爷爷删除时我使用了session.delete(对象) 当实现删除父亲我使用纯sql ...