JPA: عندما يتم إزالة كيان الوالدين ، لا يزال كيان الطفل يبقى
سؤال
كيان العميل (كيان الوالدين)
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(mappedBy="customer", cascade=CascadeType.ALL)
private List<Facility> facilities;
//Setter and Getter for name and facilities
public void addFacility(Facility facility){
if(this.facilities == null){
this.facilities = new ArrayList<Facility>();
}
this.facilities.add(facility);
facility.setCustomer(this);
}
}
كيان المنشأة (كيان الطفل)
@Entity
public class Facility {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name="CUSTOMER_FK")
private Customer customer;
private String name;
//Setter and Getter, equals and hashcode
...
}
في Customer
الكيان ، أنا أستخدم CascadeType.ALL
, ، ولكن عندما أقوم بإزالة العميل ، لا تزال المرافق المرتبطة موجودة. انا مسحت customer
بواسطة
Query query = em.createNamedQuery("Customer.delete");
query.setParameter("id", customerId);
query.executeUpdate();
أين
@NamedQuery(name="Customer.delete", query="delete from Customer c where c.id = :id")
المحلول
عمليات الحذف بالجملة ليست متتالية ، لكل مواصفات JPA:
4.10 التحديث بالجملة وحذف العمليات
...
تنطبق عملية الحذف فقط على كيانات الفئة المحددة والفئات الفرعية. لا يتسلل إلى الكيانات ذات الصلة.
...
إذا كنت ترغب في الاستفادة من التتابع ، فقم بتحميل الكيان ثم اتصل EntityManager#remove(Object)
عليه.
نصائح أخرى
محاولة مع:
@Inject
EntityManager em;
Customer customer =...;
em.remove(customer);
هذه دائما عمليات التسلل.
لا تنتمي إلى StackOverflow