How to delete a row and all its links from a database through Hibernate?

I am 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 the id and studentName properties .

The problem I am facing:

I am deleting a row from student_master via Hibernate.

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

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

How can I solve this problem?

Note. It would be great if I could overcome this problem by doing some kind of configuration with Hibernate instead of coding and request.

Edit: I saw this link. But in this he mentioned that I need to do some encoding, for example, first get all the teachers associated with the student = 1, and then remove the student = 1 from the list of students, and then update the teacher. I want to avoid this coding. Is it possible?

+6
source share
3 answers

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

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

0
source

Use the cascade="delete" property in both mapping files. This may solve your problem. If you want to assign "update", then for cascading several properties are assigned with ; .

0
source

Hibernate does not delete rows because the Student class is not aware of the relationship. To do this, you need to have a bi-directional relationship: @ ManyToMany list / teacher set and @ ManyToMany list / object set in Student class

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

But in any case, you must iterate over students and teachers to remove this student, even if the row is deleted in the database, the student object still exists, and this may cause some problems in your application. Generally speaking, take care of the relationship (both sides) before saving with Hibernate

Bidirectional ratio is easier

 for(Teacher teacher: student.teachers) { teacher.remove(student); } for(Subject subject: student.subjects) { subject.remove(student); } 
0
source

Source: https://habr.com/ru/post/952283/


All Articles