我有一个父实体ParentClass。在这个父实体中,我有一个子类列表,如下所示:-
public class BaseClass{
@Id
private Long id;
}
@Entity
public class ParentClass extends BaseClass{
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<ChildClassA> childClassAList;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<ChildClassB> childClassBList;
}
在我的数据库中,我的第三个表中只有一条 ChildClassA 记录映射到 ParentClass(假设我的parent_class 记录的 ID 为 25001,并且它们只有一个映射到此父级的子项[来自 ChildClassA 实体] 假设为 45001)。但当我调试parentClass.getchildClassAList()时,它仍然显示此列表中的2个项目(并且也具有相同的ID),而不是如下所示的单个记录:-
ParentClass parentRecord = myDao.find(ParentClass.class,25001L); //shows only one record which is expected.
List<ChildClassA> allChildsOfParent = parentRecord.getchildClassAList(); //shows 2 items in list,each with same id 45001 even though the third table in db carries one single record corresponding to this parent with id 25001.
导致此问题的可能原因是什么?任何帮助将不胜感激。
最佳答案
尝试在 @OneToMany
关系容器中使用 Set 而不是 List
关于mysql - 父级有子级列表。数据库中子项的单个记录。但在调试时它显示 2 个具有相同 id 的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23052060/