0
在開發使用JPA的EclipseLink的實現一個Eclipse GEF應用我發現,一直討厭我,而一個錯誤:JPA多對多關係更新失敗,原因是在另一個關係約束鍵
我有三個不同的類別:
第一個代表包含在一個模型中的變量:
@Entity
public class Variable extends AbstractVariable{
@Id
@Generated value
private int id;
/** Lots more of stuff */
@ManyToOne(cascade=CascadeType.ALL)
Model model;
//Setters, getters and the other functions.
}
而abstractvariant類,它是能容納的變量的串聯的可變的另一亞類。
@Entity
public class VariableList extends AbstractVariable{
@Id
@Generated value
private int id;
/** Lots more of stuff */
@ManyToMany(cascade=CascadeType.ALL)
List<AbstractVariable> variables;
//Setters, getters and the other functions.
}
第二類,可以容納變量值的gef editpart。
@Entity
public class VariableEditPart{
@Id
@Generated value
private int id;
/** Lots more of stuff */
VariableList vars;
//Setters, getters and the other functions.
}
而且與全球環境基金模型中的最後一堂課:
@Entity
public class Model{
@Id
@Generated value
private int id;
/** Lots more of stuff */
@OneToMany(cascade=CascadeType.ALL)
List<Variable> availableVars;
@OneToMany(cascade=CascadeType.ALL)
List<VariableEditPart> editParts;
//Setters, getters and the other functions.
}
這裏的問題是,JPA創建關係的variablelist變量表,並用的EditPart和的variablelist另一個關係,所以,只要我試圖在數據庫更新模型後進行一些修改,它會自動刪除變量,並最終導致違反約束的錯誤,因爲模型所保留的變量列表仍指向該變量(它順便說一下,我並沒有假裝刪除,我已經測試了很多不同的cascadeType,以避免它沒有任何運氣。 )。
感謝您的關注,並善待我的英語,這不是我的母語;)
我重新審視了這個模型,因爲最近由於客戶做了修改而變得有點麻煩,最後問題解決了。 –