The most common cause of the problem is that you're trying to remove from data base an object that is still referenced from parent's collection (usually annotated with @OneToMany(cascade = CascadeType.ALL)). Here is a quick sample of such relation:
@Entity
public class Course implements Serializable {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "course")
private List classes;
....
}
@Entity
public class Clazz implements Serializable {
@ManyToOne
@JoinColumn(name = "COURSE_ID")
private Course course;
...
}
When you know what's causing the problem fixing it is very simple. You need to remove an object from parent's collection before deleting it in database:
course.getClasses().remove(clazz);
entityManager.remove(clazz);
No comments:
Post a Comment