我有這三個實體,在DB中有自己的表。主要實體是Wearable
和Job
。請記住,此代碼中設置的CascadeType是錯誤的。JPA,三個實體,它們的關係。 CascadeType正確刪除?
@Entity
public class Wearable {
@Id
private String mac;
@OneToMany(mappedBy = "wearable")
private List<Job> jobs;
@OneToMany(mappedBy = "wearable", cascade = {CascadeType.ALL})
private List<WearableSubscription> subscriptions;
}
@Entity
public class Job {
@Id
private String id;
@OneToOne(mappedBy = "job", cascade = CascadeType.ALL)
private WearableSubscription wearableSubscription;
}
然後我想囑咐Wearable
和Job
之間的關係(WearableSubscription
)。可穿戴設備可以有很多WearableSubscription
,但是WearableSubscription
中只有一個可以有一個Job
。
@Entity
public class WearableSubscription {
private WearableSubscriptionId id;
@OneToOne (cascade = {CascadeType.ALL, CascadeType.PERSIST})
@JoinColumn(name = "job_id", insertable = false, updatable = false)
private Job job;
@ManyToOne(cascade = {CascadeType.ALL, CascadeType.PERSIST})
@JoinColumn(name = "wearable_mac", insertable = false, updatable = false)
private Wearable wearable;
private String message;
private String imageUrl;
private String audioUrl;
}
所以在這一點上,我不知道確切的CascadeType是如何工作的。我的意思是,如果我想刪除WearableSubscription
而不刪除Wearable
和Job
,那麼在所有這些字段中我必須設置哪些類型的級聯?我嘗試了所有的組合,並且在某些情況下我刪除了所有的(WSub,W和J),而在其他情況下,當我執行刪除時,什麼都沒有發生,所有(WSub,W和J)仍然在數據庫中。