【问题标题】:java.lang.IllegalArgumentException: Removing a detached instance of StudentSubjectjava.lang.IllegalArgumentException:删除 StudentSubject 的分离实例
【发布时间】:2015-04-12 21:45:32
【问题描述】:

我在层次结构中有三个实体 1) 教师 2) 学生 3) 科目 在教师实体中,与学生类存在 OneToMany 关系。 Student 类与 Subject 类具有 OneToMany 关系。 我有一个特定的情况,老师有 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 {


}

非常感谢任何帮助。

【问题讨论】:

    标签: java hibernate jpa


    【解决方案1】:

    在调试和探索更多之后,我找到了这个问题的解决方案。 在从父母列表中删除学生实体之前,我刷新了该实体。 StudentSubject 数组的所有引用随后都被激活,进而解决了在保存 Teacher 对象时实体分离的问题。

    【讨论】:

      猜你喜欢
      • 2013-06-06
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 2021-09-12
      相关资源
      最近更新 更多