have three entities in hierarchy 1)Teacher 2)Student 3)Subject In Teacher Entity there OneToMany relationship to Student class. Student class has OneToMany relationship with Subject class. I have specific situation in which Teacher has 5 students and students have many subjects assigned to them. I am fetching entity of Teacher and fetching list of Student from teacher entity. Out of 5, I am removing one Student entity from list using remove() function. While saving Teacher entity, getting error as
java.lang.IllegalArgumentException: Removing a detached instance of StudentSubject Here StudentSubject is child entity of Student.
Code:
public class Teacher {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="studentCode", orphanRemoval = true)
@org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SELECT)
@org.hibernate.annotations.BatchSize(size=30)
private List<Student> students = new ArrayList<Student>();
}
public class Student {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="studentSubjectCode", orphanRemoval = true)
@org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SELECT)
@org.hibernate.annotations.BatchSize(size=30)
private List<StudentSubject> studentsubjects = new ArrayList<StudentSubject>();
}
public class StudentSubject {
}