1
私は、hibernate entitymanagerを持つprbolemを持っています。特に、命令removeを使用しています。命令によるHibernateの問題の除去
データベースからエンティティを削除しようとしましたが、システムはこのエラーを返します。
ERROR [JDBCExceptionReporter] Cannot delete or update a parent row: a foreign key constraint fails (`datalesson`.`coursematerial_lecture`, CONSTRAINT `FK2471D4A14EC7B08F` FOREIGN KEY (`lectures_id`) REFERENCES `lecture` (`id`))
このerroreを生成するラインコードは、これは、次のとおりです。
private static CleanDatabaseSystemRemote cdsr;
cdsr = (CleanDatabaseSystemRemote) ctx.lookup("CleanDatabaseSystemJNDI");
...
int idCourse = tsr.CreateCourse("Test1", "JUnitTest1", 10, idTrainer);
int idCourseMaterial = tsr.CreateCourseMaterial(idCourse, idTrainer, 1, "CourseMaterial");
int idLecture = tsr.CreateLecture(idCourseMaterial, "Test");
...
cdsr.removeCourseMaterial(idCourseMaterial);
cdsr.removeLecture(idLecture);
cdsr.removeCourse(idCourse);
CleanDatabaseSystemています
@Remove
public void removeCourse(int idCourse) {
Course course = new Course();
course = manager.find(Course.class, idCourse);
if(course != null){
manager.remove(course);
}
}
@Remove
public void removeCourseMaterial(int idCourseMaterial) {
CourseMaterial courseMaterial = new CourseMaterial();
courseMaterial = manager.find(CourseMaterial.class, idCourseMaterial);
if(courseMaterial != null){
manager.remove(courseMaterial);
}
}
@Remove
public void removeLecture(int idLecture) {
Lecture lecture = new Lecture();
lecture = manager.find(Lecture.class,idLecture);
if (lecture != null) {
manager.remove(lecture);
}
}
そして、エンティティが、私はしません
@Entity
public class Course implements java.io.Serializable{
...
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getCredits() {
return credits;
}
public void setCredits(int credits) {
this.credits = credits;
}
@ManyToOne(cascade={CascadeType.ALL})
public Trainer getTrainer() {
return trainer;
}
public void setTrainer(Trainer trainer) {
this.trainer = trainer;
}
@ManyToMany(targetEntity = lesson.domain.Trainee.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinTable(name="COURSE_TRAINEE", joinColumns= @JoinColumn(name="COURSE_ID", unique=false), [email protected](name="TRAINEE_ID", unique=false))
public Set<Trainee> getStudents() {
return students;
}
public void setStudents(Set<Trainee> students) {
this.students = students;
}
}
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class CourseMaterial extends LearningObject implements java.io.Serializable{
...
@OneToMany(cascade={CascadeType.ALL})
public Set<Lecture> getLectures() {
return lectures;
}
public void setLectures(Set<Lecture> lectures) {
this.lectures = lectures;
}
public String getMaterialName() {
return materialName;
}
public void setMaterialName(String materialName) {
this.materialName = materialName;
}
public void insertLecture(Lecture lecture){
this.lectures.add(lecture);
}
}
@Entity
public class Lecture implements java.io.Serializable {
...
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
@OneToMany(cascade={CascadeType.ALL})
public Set<Papers> getPapers() {
return papers;
}
public void setPapers(Set<Papers> papers) {
this.papers = papers;
}
@OneToMany(cascade={CascadeType.ALL})
public Set<Slide> getSlides() {
return slides;
}
public void setSlides(Set<Slide> slides) {
this.slides = slides;
}
@OneToMany(cascade={CascadeType.ALL})
public Set<Example> getExamples() {
return examples;
}
public void setExamples(Set<Example> examples) {
this.examples = examples;
}
public void insertPapers(Papers papers){
this.papers.add(papers);
}
public void insertSlide(Slide slide){
this.slides.add(slide);
}
public void insertExample(Example example){
this.examples.add(example);
}
}
です問題を理解する。 カスケードはありますか?注釈がありません。あなたはおそらく最初の講義を削除することをお勧めします、そしてもちろん材料の二 -
おかげ