开发者

JPA ManyToMany relation update failed due to constraint key in another relation

开发者 https://www.devze.com 2023-04-10 18:05 出处:网络
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:

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 variab开发者_如何学编程le (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 ;)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号