Java oneTomany 联级删除

hibernate cascade属性 all-delete-orphan
@oneTomany
要联级删除必须要加上
cascade 为 all-delete-orphan


@Entity
@Table(name = "ORDERS")
public class Order implements Auditable, Serializable
{
    ...
  
    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    private List<OrderItem> orderItems;
  
    ...
}

注意@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)这句。经测试后确认,加入这句后,JPA底层的Hibernate就会完成delete-orphan的动作,没有这句则不行。

你可能感兴趣的:(Hibernate)