orphanremoval(JPA中orphanRemoval 属性有什么用)

发布时间:2025-12-11 02:16:58 浏览次数:2

JPA定义Model关系中有orphanRemoval 这个参数。其作用您是否了解?

  • orphanRemoval介绍和作用

上面截图的jpa的官方文档以及jpa规范中明确说明,如果javaBean中父实体和子实体之间有一对一或一对多的级联关系的时候,如果我们想要删除父实体,也必须要级联删除子实体,需要被删除的级联关系中的子实体则被称为孤儿实体。

orphanremoval属性的主要作用就是标记是否可以删除孤儿实体,假设我们这里有一个订单的案例,订单下面有许多行记录,假设我们删除其中的一行记录的话,需要被删除的这一行记录就被称作是孤儿,当我们设置这个属性为true的时候,意思就是我们可以从这个订单的所有记录中删除标记为孤儿的这条记录。

  • 属性详解和案例代码

@OneToMany(mappedBy="parent",targetEntity=Child.class,cascade={CascadeType.ALL},orphanRemoval=true)privateList<Child>children=newArrayList<Child>();@OneToMany(mappedBy="parent",targetEntity=Son.class,cascade={CascadeType.ALL},orphanRemoval=true)privateList<Son>son=newArrayList<Son>();Fistly,wesaveOneParenthavetwoSonandtwoChildindatabase:Parentp=newParent();Childc1=newChild();Childc2=newChild();Sons1=newSon();Sons2=newSon();p.setParentName("AAA");c1.setChildName("BBB");c2.setChildName("CCC");s1.setName("s1");s2.setName("s2");//setrelationshipp.getChildren().add(c1);p.getChildren().add(c2);p.getSon().add(s1);p.getSon().add(s2);c1.setParent(p);c2.setParent(p);s1.setParent(p);s2.setParent(p);em.merge(p);sowecanseeitindatabase:mysql>select*fromParent;+----+------------+|id|parentName|+----+------------+|1|AAA|+----+------------+mysql>select*fromChild;+----+-----------+-----------+|id|childName|parent_id|+----+-----------+-----------+|1|BBB|1||2|CCC|1|+----+-----------+-----------+mysql>select*fromSon;+----+------+-----------+|id|name|parent_id|+----+------+-----------+|1|s1|1||2|s2|1|+----+------+-----------+thenwecanusemergetoremoveorphanedentities.Atfirst,weshouldceateatestData.Parentp=newParent();//setidtotheentity,whenthereiscorrespongdingrecordindatabase,justupdate.p.setId(1);Childc1=newChild();c1.setId(1);c1.setChildName("c3");Sons2=newSon();s2.setId(2);s2.setName("c4");//newChildandSonEnity,andaddittoParent'sList.Withoutid,sojpawillsaveanewrecordforthem,idauto-increment.Childc=newChild();c.setChildName("childNew");c.setParent(p);p.getChildren().add(c);Sons=newSon();s.setName("sonNew");s.setParent(p);p.getSon().add(s);//atlast,mergetherootEntityParent,whenwesettheorphanRemovaltruein@OneToMany,theChild(id=2)Son(id=1)entitywillbedeletedwhenthelineitemisremovedfromtheorder.em.merge(p);thenwecanseeitindatabase:mysql>select*fromParent;+----+------------+|id|parentName|+----+------------+|1|AAAnew|+----+------------+mysql>select*fromChild;+----+-----------+-----------+|id|childName|parent_id|+----+-----------+-----------+|1|c3|1||3|childNew|1|+----+-----------+-----------+recordwithid=2isremoved.mysql>select*fromSon;+----+--------+-----------+|id|name|parent_id|+----+--------+-----------+|2|c4|1||3|sonNew|1|+----+--------+-----------+recordwithid=1isremoved.
  • 与cascade的关系

二者的作用范围不一样,cascade的作用范围是数据库,当cascade属性设置了delete时,当删除级联关系中的子集时,顺便也会将数据库中对应的数据删除。orphanremoval属性的作用范围仅仅是java应用代码中,做级联删除的操作也只适用于java实体代码范畴,它可以清楚javabean的级联关系,但并不能影响数据库的数据,只要cascade不点头是无法删除掉数据库的数据的

上述就是小编为大家分享的JPA中orphanRemoval 属性有什么用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注本站行业资讯频道。

orphanremoval
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477