由于另一个关系中的约束键,JPA ManyToMany 关系更新失败
在使用 JPA 的 eclipselink 实现开发 Eclipse GEF 应用程序时,我发现了一个让我烦恼了一段时间的错误:
我有三个不同的类:
第一个代表模型中包含的变量:
@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.
}
GEF 模型的最后一课:
@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 为关系变量列表变量创建一个表,以及与 editpart 和变量列表的另一个关系,因此,一旦我尝试在数据库中更新模型,进行一些修改,它会尝试自动删除变量,并最终导致约束违规错误,因为模型保存的变量列表仍然指向该变量(顺便说一句,我并没有假装删除,而且我'我们测试了很多不同的cascadeType 是为了避免它而没有任何运气......)。
感谢您的关注并善待我的英语,这不是我的母语;)
While developing an Eclipse GEF application using an eclipselink implementation of JPA i have found an error that has been annoying me for a while:
I have three different classes:
The first one represents a variable contained in a model:
@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.
}
And another subclass of the abstractvariant class, which is a variable which can hold a concatenation of variables.
@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.
}
The second class, a gef editpart that can hold a variable value.
@Entity
public class VariableEditPart{
@Id
@Generated value
private int id;
/** Lots more of stuff */
VariableList vars;
//Setters, getters and the other functions.
}
And a last class with the gef model:
@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.
}
The issue here is that JPA creates a table for the relation variablelist-variable, and another relation with the editpart and the variablelist, so, as soon as I try to update the model at the database after some modifications, It tries automatically to delete the Variable, and ends up with a constraint violation error caused because the list of variables holded by the model still points to that variable (which by the way, I was not pretending to delete, and I've tested lots of differenst cascadeType's to avoid it without any luck...).
Thanks for your attention and be kind with my english, it's not my native language ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来你有一个非常相互关联的模型,所有东西都循环引用所有东西。
你到底在做什么?当您删除变量时,是否删除了对它的所有引用?你需要这样做。
It seems you have a very interrelated model with everything referencing everything in cycles.
What are you doing exactly? When you remove the Variable, are you removing all references to it? You need to.