java.lang.IllegalArgumentException:删除 StudentSubject 的分离实例
java.lang.IllegalArgumentException: Removing a detached instance of StudentSubject
我在层次结构中有三个实体 1)教师 2)学生 3)主题
在 Teacher 实体中,存在与 Student class 的 OneToMany 关系。学生 class 与主题 class 具有一对多关系。
我的具体情况是老师有 5 个学生,学生有很多科目分配给他们。我正在获取教师实体并从教师实体中获取学生列表。在 5 个实体中,我使用 remove() 函数从列表中删除了一个学生实体。保存教师实体时,出现错误
**java.lang.IllegalArgumentException: 删除 StudentSubject 的分离实例 ** 这里 StudentSubject 是 Student 的子实体。
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 {
}
非常感谢任何帮助。
在调试和探索更多之后,我找到了解决这个问题的方法。
在从家长列表中删除学生实体之前,我刷新了该实体。然后 StudentSubject 数组的所有引用都变为活动状态,从而解决了保存 Teacher 对象时分离实体的问题。
我在层次结构中有三个实体 1)教师 2)学生 3)主题 在 Teacher 实体中,存在与 Student class 的 OneToMany 关系。学生 class 与主题 class 具有一对多关系。 我的具体情况是老师有 5 个学生,学生有很多科目分配给他们。我正在获取教师实体并从教师实体中获取学生列表。在 5 个实体中,我使用 remove() 函数从列表中删除了一个学生实体。保存教师实体时,出现错误 **java.lang.IllegalArgumentException: 删除 StudentSubject 的分离实例 ** 这里 StudentSubject 是 Student 的子实体。
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 {
}
非常感谢任何帮助。
在调试和探索更多之后,我找到了解决这个问题的方法。 在从家长列表中删除学生实体之前,我刷新了该实体。然后 StudentSubject 数组的所有引用都变为活动状态,从而解决了保存 Teacher 对象时分离实体的问题。