Hibernate Cascade:在没有子实体的情况下保存父实体时删除子实体

时间:2018-04-27 07:04:00

标签: hibernate jpa spring-data-jpa

所以我有一个schedule对象,其中包含DateActiveScheduleItem的列表。我想要它,这样如果我从schedule对象的列表中删除DateActiveScheduleItems项并使用CrudRepository保存该计划,它将从数据库中删除DateActiveScheduleItem,at我的代码似乎没那样做的那一刻。我该如何改变级联?

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            float offsetAlpha = (appBarLayout.getY() / appBarLayout.getTotalScrollRange());
            ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(imageView, "scaleX", offsetAlpha + 1);
            ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(imageView, "scaleY", offsetAlpha + 1);
            scaleDownX.setDuration(0).start();
            scaleDownY.setDuration(0).start();
}

DateActiveScheduleItem

{{1}}

1 个答案:

答案 0 :(得分:0)

孤立删除会从关系中删除相应的子项时删除相应的子项。因此,如果从dateActiveScheduleItems集合中删除1个DateActiveScheduleItem,JPA也会自动从数据库中删除该DateActiveScheduleItem。

@OneToMany(mappedBy = "schedule",cascade = CascadeType.ALL, orphanRemoval="true")
private List<DateActiveScheduleItem> dateActiveScheduleItems = new ArrayList<>();

有关OrphanRemovel的更多信息HERE