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