[英]JPA delete fails (integrity constraint violation: foreign key no action) - data model too convoluted?
I'm experiencing a problem I cannot get my head around. 我遇到了一个问题,我无法理解。 I really hope someone out there can help. 我真的希望那里的人可以提供帮助。
This might get a little meta as my data model represents programming objects. 当我的数据模型代表编程对象时,这可能会得到一点元。 I've got three entities: Program, Clazz and Method. 我有三个实体:Program,Clazz和Method。 Classes can have multiple methods and occasionally inherited some other methods (from other classes). 类可以有多个方法,偶尔会继承一些其他方法(来自其他类)。
@Entity
public class Program {
@OneToMany(mappedBy="program", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@OrderBy("name ASC")
private Set<Clazz> clazzes = new TreeSet<Clazz>();
}
@Entity
public class Clazz {
@ManyToOne
private Program program;
@OneToMany(mappedBy="clazz", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@OrderBy("name ASC")
private Set<Method> methods = new TreeSet<Method>();
@ManyToMany(fetch=FetchType.LAZY, mappedBy="inheritedBy")
@OrderBy("name ASC")
private Set<Method> inheritedMethods = new TreeSet<Method>();
@PreRemove
private void tearDown() {
inheritedMethods.clear();
}
}
@Entity @Table(name="sf_method")
public class Method {
@ManyToOne(optional=true)
private Clazz clazz;
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name = "sf_method_inherited_by_class",
joinColumns = { @JoinColumn(name = "sf_method_inherited_by_class") },
inverseJoinColumns = { @JoinColumn(name = "sf_class_inherits_method") }
)
private Set<Clazz> inheritedBy = new HashSet<Clazz>();
@PreRemove
public void tearDown() {
inheritedBy.clear();
}
}
What I need to do is to delete a program, including all its classes and methods. 我需要做的是删除一个程序,包括它的所有类和方法。 I use Spring-JPA repositories, so my code is: 我使用Spring-JPA存储库,所以我的代码是:
@Autowired ProgramRepository programs;
@Transactional(readOnly=false)
public void forceDelete(Program p) {
Set<Clazz> myClasses = p.getClazzes();
for (Clazz c : myClasses) {
logger.info("Clazz " + c.getName());
for (Method m : c.getMethods()) {
m.setClazz(null);
methods.save(m);
}
}
programs.delete(p);
}
The delete code works only in some circumstances. 删除代码仅在某些情况下有效。 Sometimes I get the following error. 有时我会收到以下错误。
SqlExceptionHelper - integrity constraint violation: foreign key no action; SqlExceptionHelper - 完整性约束违规:外键无动作; FKIBMDD2FV8TNJAF4VJTOEICS73 table: SF_METHOD AbstractBatchImpl - HHH000010: On release of batch it still contained JDBC statements FKIBMDD2FV8TNJAF4VJTOEICS73表:SF_METHOD AbstractBatchImpl - HHH000010:在批量发布时它仍然包含JDBC语句
Any idea? 任何想法? I spent so many hours on this and I failed every single resolution attempt. 我花了这么多时间在这上面,我每次尝试解决都失败了。 What am I doing wrong? 我究竟做错了什么?
Here you are trying to delete the parent without deleting the child which have foreign key referencing to the parent. 在这里,您尝试删除父项而不删除具有引用父项的外键的子项。
Here, before you delete p you have to delete children for p. 在这里,在删除p之前,你必须删除p的子节点。 You can get that by : p.getClazzes(); 你可以通过:p.getClazzes();
Again before deleting Clazz, you have to delete children for it(in this case, Method) as above... 在删除Clazz之前,你必须删除它的子节点(在本例中为Method),如上所述...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.