2010-11-11 139 views
0

我是新的休眠。所以,我不知道如何做到這一點:休眠:從關聯表中刪除記錄與外鍵

我有3個表:

表人:

@Entity 
@Table(name = "ASD_PERSON") 
public class AsdPerson implements Serializable { 
    @Id 
    @SequenceGenerator(name="seq_name", sequenceName="gen_id_value", allocationSize = 1) 
    @GeneratedValue(generator="seq_name") 
    @Column(name="F_PERSON_ID", nullable = false) 
    private Long fPersonId; 

    @OneToMany(mappedBy = "AsdPerson", 
       cascade = CascadeType.ALL, 
       orphanRemoval = true) 
    private List<AsdPersonEvent> asdPersonEventList; 

    ... setters and getters ... 
} 

表事件:

@Entity 
@Table(name = "ASD_EVENT") 
public class AsdEvent implements Serializable { 
    @Id 
    @SequenceGenerator(name="seq_name", sequenceName="gen_id_value", allocationSize = 1) 
    @GeneratedValue(generator="seq_name") 
    @Column(name="F_EVENT_ID", nullable = false) 
    private Long fEventId; 

    @OneToMany(mappedBy = "AsdEvent", 
       cascade = CascadeType.ALL, 
       orphanRemoval = true) 
    private List<AsdPersonEvent> asdPersonEventList; 

    ... setters and getters ... 
} 

表與人事件:

@Entity 
@Table(name = "ASD_PERSON_EVENT") 
@IdClass(AsdPersonEventPK.class) 
public class AsdPersonEvent implements Serializable { 
    @Id 
    @GenericGenerator(name = "generator", strategy = "foreign", 
         parameters = @Parameter(name = "property", value = "asdPerson")) 
    @GeneratedValue(generator = "generator") 
    @Column(name="F_PERSON_ID", nullable = false, insertable = false, 
      updatable = false) 
    private Long fPersonId; 

    @Id 
    @GenericGenerator(name = "generator", strategy = "foreign", 
         parameters = @Parameter(name = "property", value = "asdEvent")) 
    @GeneratedValue(generator = "generator") 
    @Column(name="F_EVENT_ID", nullable = false, insertable = false, 
      updatable = false) 
    private Long fEventId; 

    @ManyToOne 
    @JoinColumn(name = "F_PERSON_ID", insertable = false, 
       updatable = false) 
    private AsdPerson asdPerson; 

    @ManyToOne 
    @JoinColumn(name = "F_EVENT_ID", insertable = false, 
       updatable = false) 
    private AsdEvent asdEvent; 

    ... setters and getters ... 
} 

Everything works完美(添加新記錄,創建新的對象)的情況下除外,當我嘗試刪除事件表或Person表相關的記錄:

... 
AsdEvent ev = getService().get(115); // get record from Event table by id = 115 (for example) 
ev.getAsdPersonEventList().remove(1); // delete some existing records 
getService().merge(ev); 
... 

之後,我得到的錯誤:

deleted object would be re-saved by cascade (remove deleted object from associations): [database.AsdPersonEvent#[email protected]]

如何配置Hibernate註釋或其他方式來擺脫這個錯誤?

+0

請嘗試@Cascade({CascadeType.SAVE_UPDATE,CascadeType.DELETE_ORPHAN}),並顯示如何從列表中刪除AsdPersonEvent。 – blow 2010-11-11 10:49:05

+0

我已經展示瞭如何從列表中刪除AsdPersonEvent。 – brazh 2010-11-11 11:00:43

+0

@blow:不,它不起作用。我不明白,當你說:「顯示如何從列表中刪除AsdPersonEvent」時,你的意思是什麼。 – brazh 2010-11-11 12:25:03

回答

0

通過去除orphanRemoval = true

+0

我試過了,但它沒有幫助我。沒有錯誤,但記錄未被刪除。 – brazh 2010-11-11 10:27:57

2

如果你有持久實體的複雜的圖形再試一次,我想你需要放棄使用orphanRemoval和使用em.remove()手動刪除您的實體。

orphanRemoval是專爲簡單的父母與子女的關係,其中孩子沒有父母沒有意義。如果在你的情況下,孩子可能有其他的關係,也許這不是orphanRemoval的好例子。

+0

當我保存對象ev時,它具有結構ev-> AsdPersonEvent(not null) - > AsdPerson(not null) - > AsdPersonEvent(null)。我可以在保存ev之前手動刪除AsdPersonEvent中的記錄,但我認爲有更合適的Hibernate註釋解決方案。 – brazh 2010-11-11 13:25:13

+0

@brazh:已更新。 – axtavt 2010-11-11 13:34:15

+0

感謝您的回答。到目前爲止,我手動從AsdPersonEvent中刪除記錄,但如果有更好的解決方案,我會很樂意使用它:) – brazh 2010-11-11 14:26:25