在我的應用程序中,我想將應用程序中的所有圖像放在一張表中。我上次發布了一個問題,有人建議我使用unidirectional @OneToMany。我有以下與Image實體關聯的實體@Entity@Table(name="promotion")public class Promotion { @Id @Column(name="id") protected String id; @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn(name="itemId") protected List<Image> images = new ArrayList<>(); }@Entity@Table(name="product")public class Product { @Id @Column(name="id") protected String id; @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn(name="itemId") protected List<Image> images = new ArrayList<>(); }@Entity@Table(name="image")public class Image{ @Id @Column(name="id") private String id = IdGenerator.createId(); @Column(name="itemId") private String itemId; @Column(name="title", nullable = false) protected String title; @Column(name="filename", nullable = false) protected String filename; @Column(name="path", unique = true) private String path; @Column(nullable = true) protected int width; @Column(nullable = true) protected int height;}我現在面臨的問題是:A) 當我cascade={CascadeType.PERSIST, CascadeType.MERGE}在圖像 ArrayList屬性上使用時,出現此異常:org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1所以我用它替換了它,cascade=CascadeType.ALL當我保存Product或Promotion相關的Image(s)也被保存時,這很酷,這就是我想要的B) 我現在遇到的主要問題是當我刪除 aProduct或 時Promotion,其關聯的圖像永遠不會被刪除。它們的關聯圖像保留在圖像表中。通過使用cascade=CascadeType.ALL我希望當我刪除 aProduct或 a 時Promotion,它的圖像也應該被自動刪除。我試圖從數據庫中刪除一個圖像,如果它會觸發它的關聯Product或被Promotion刪除,但它沒有,因為我認為它是單向的,這是有道理的。但是為什么當我刪除 aProduct或其Promotion關聯的圖像時不會被刪除
添加回答
舉報
0/150
提交
取消