G-programmer
G-programmer

Reputation: 127

I cannot remove the association in Many To Many bidirectional hibernate

I can't delete the association in the courses_student table of course and student when trying to delete a course, even if I want to cascade delete it does not work for me since there is a foreign key in courses_student, I don't know what the problem is.

I have also tried to remove the association in the courses_student table doing a update.but nothing happened.

DAO

@Override
public boolean deleteCourse(int id) {
    Session currentSession = entityManager.unwrap(Session.class);
    Courses course = currentSession.load(Courses.class, id);

    for(Student student : course.getEstudiantes()) {
        course.removeStudent(student);
    }

    currentSession.delete(course);
    
    if(course.getId() == null)
        return true;
    
    else
        return false;
}

Courses entity

@Entity
@Table(name = "courses")
public class Courses {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    @Column
    private String nombre;
    @Column
    private String descripcion;
    
    @ManyToMany(mappedBy = "courses")
    private Set<Student> Estudiantes = new HashSet<Student>();
    
    public Courses() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public Set<Student> getEstudiantes() {
        return Estudiantes;
    }

    public void setEstudiantes(Set<Student> estudiantes) {
        Estudiantes = estudiantes;
    }
    
    public void removeStudent(Student student) {
        this.Estudiantes.remove(student);
        student.getCourses().remove(this);
    }
    

}

Student entity

@Entity
@Table(name = "students")
public class Student {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    @Column
    private String nombre;
    @Column
    private String apellido;
    @Column
    private String dni;
    
    @ManyToMany(fetch=FetchType.LAZY,
            cascade= {CascadeType.PERSIST, CascadeType.MERGE,
             CascadeType.DETACH, CascadeType.REFRESH})
    @JoinTable(
            name="courses_students",
            joinColumns=@JoinColumn(name="id_student"),
            inverseJoinColumns=@JoinColumn(name="id_course")
            )
    private Set<Courses> courses = new HashSet<Courses>();
    
    public Student() {
    }
    
    public Student(String nombre, String apellido, String dni) {
        this.nombre = nombre;
        this.apellido = apellido;
        this.dni = dni;
    }



    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getApellido() {
        return apellido;
    }

    public void setApellido(String apellido) {
        this.apellido = apellido;
    }

    public String getDni() {
        return dni;
    }

    public void setDni(String dni) {
        this.dni = dni;
    }

    public Set<Courses> getCourses() {
        return courses;
    }

    public void setCourses(Set<Courses> courses) {
        this.courses = courses;
    }

}

EDIT: apparently it works for me, trying to update since owner side.

@Override
public boolean deleteCourse(int id) {
    Session currentSession = entityManager.unwrap(Session.class);
    Courses course = currentSession.load(Courses.class, id);
    
    for(Student student : course.getEstudiantes()) {
        student.removeCourse(course);
    }
    
    currentSession.update(course);

    if(course.getId() == null)
        return true;
    
    else
        return false;
}

Upvotes: 0

Views: 423

Answers (1)

Jo&#227;o Dias
Jo&#227;o Dias

Reputation: 17460

It seems to me that you are missing a cascade configuration of your @ManyToMany annotation on Courses which is actually the one you are updating / deleting. Try the following:

@ManyToMany(mappedBy = "courses", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private Set<Student> Estudiantes = new HashSet<Student>();

Also, given that you have a bi-directional relationship, you should also remove the Course from each Student courses property.

Upvotes: 1

Related Questions