【问题标题】:Hibernate problem with instruction remove指令删除的休眠问题
【发布时间】:2011-06-12 20:27:10
【问题描述】:

我有一个休眠实体管理器的问题,特别是删除指令。

我尝试从数据库中删除一个实体,但系统返回此错误。

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`))

产生此错误的行代码如下:

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), inverseJoinColumns=@JoinColumn(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);
    }

}

我不明白这个问题。 是级联吗?我错过了任何注释?

谢谢

【问题讨论】:

    标签: mysql hibernate jakarta-ee ejb


    【解决方案1】:

    看起来您的课程资料是从讲座中引用的 - 您可能希望先删除讲座,然后再删除课程资料。

    【讨论】:

      猜你喜欢
      • 2011-02-16
      • 1970-01-01
      • 2015-05-31
      • 2013-02-15
      • 2015-08-01
      • 2019-07-31
      • 2012-09-17
      相关资源
      最近更新 更多