简体   繁体   中英

How to delete a row & all its references from the database through Hibernate?

I'm using Hibernate 3.0 with Java.

I have the following classes.

Teacher.java

private long id;
private String teacherName;
private List<Student> students;
// getter-setter of all

Subject.java

private long id;
private String subjectName;
private List<Student> students;
// getter-setter of all

Student.java

 private long id;
 private String studentName;

// getter-setter of both

Teacher.hbm.xml

<class name="Teacher" table="teacher_master">
    <!--other mappings-->

    <list name="students" cascade="refresh" table="teacher_student_master">
        <key column="teacher_id"/>
        <index column="student_teacher_position" type="integer"/>
        <many-to-many class="Student" column="student_id"/>
    </list>
</class>

Subject.hbm.xml

<class name="Subject" table="subject_master">
    <!--other mappings-->

    <list name="students" cascade="refresh" table="subject_student_master">
        <key column="subject_id"/>
        <index column="student_subject_position" type="integer"/>
        <many-to-many class="Student" column="student_id"/>
    </list>
</class>

Student.hbm.xml contains mappings for id & studentName properties.

Problem I'm facing is:

I delete a row from student_master through Hibernate.

Student stu = new Student();
stu.setId(1l);
session.delete(stu);
transaction.commit();

But the references of the deleted student (id = 1) are not deleted from the tables teacher_student_master and subject_student_master .

How can I overcome this issue?

Note: It would be great if I can overcome this issue by doing some kind of configuration with Hibernate instead of coding & query firing.

Edit: I've seen this link . But in that, it's mentioned that I need to do some coding like, first get all the teachers associated with student=1, then remove the student=1 from the list of student & then update teacher. I want to avoid this coding. Is it possible?

You can use @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN) to the columns you want this behavior. See this question for more information.

Or, since you are using Hibernate configuration through XML, use cascade="delete-orphan". Take a look at this blog post where different types of cascades are explained.

Use cascade="delete" property in both mapping files. It might solve your problem. If you want to assigned "refresh" then assigned multiple properties to cascade using ; .

Hibernate don't delete the rows because the Student class is not aware of the relationships. To do that you should have bi-directional relationships: a @ManyToMany List/Set of Teacher and a @ManyToMany List/Set of Subject in the Student class

Or, at least, a database delete cascade on the foreign key student_id of the tables teacher_student_master and subject_student_master will delete the rows in the database (I think the first solution is better).

But, in any case, you should iterate over the student's subjects and teachers to remove this student, even if the row are deleted in the database, the student object is still there and it could raise some issues in your application... Generally speaking take care of relationships (both side) before saving with Hibernate

With a bidirectional relationship it's easier

for(Teacher teacher: student.teachers) {
    teacher.remove(student);
}

for(Subject subject: student.subjects) {
    subject.remove(student);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM