JPA继承-我想删除带有子行的父行,该如何操作

fafcakar  于 2022-11-14  发布在  其他
关注(0)|答案(2)|浏览(128)

父类

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class OnlinePost {
    @Id
    @SequenceGenerator(
            name = "onlinePost_sequence",
            sequenceName = "onlinePost_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "onlinePost_sequence"
    )
    private Long postId;
    // some other columns, getters and setters

}

子类别

public class Post extends OnlinePost{
    private String image;
    private String title;
}

我要删除父元素,然后自动删除子元素

b09cbbtk

b09cbbtk1#

您可以执行以下操作。

@Entity
public class OnlinePost {
    @Id
    @SequenceGenerator(
            name = "onlinePost_sequence",
            sequenceName = "onlinePost_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "onlinePost_sequence"
    )
    private Long postId;
    // some other columns, getters and setters

    @OneToOne(cascade= CascadeType.DELETE)
    Post post;
}

public class Post{
    private String image;
    private String title;
}

删除父项时执行此操作。子项将自动删除

2vuwiymt

2vuwiymt2#

在父类中创建一个子字段,然后Map两个类并在关系中应用级联类型删除,以获得所需的输出示例:

//for One to one:

@OneToOne(cascade= CascadeType.DELETE)
private Post post;

相关问题