我是 Spring 和 JPA 的新手。我有一个 parent class
叫parent
具有属性
@OneToMany(mappedBy="father")
private List<Student> children = new ArrayList<>();
还有一个class Student
与属性
@ManyToOne
private Parent father;
@ManyToOne
private Parent mother;
当我想删除父亲时,它可以工作,但不能删除母亲。
我确实想保留这两个属性 father
和mother
并将它们都映射到 childen
请问有什么建议吗?谢谢
最佳答案
您可以定义两个 child 列表,一个用于父亲,一个用于母亲,形成双向关系
更改:
@OneToMany(mappedBy="father")
private List<Student> children = new ArrayList<>();
致:
@OneToMany(mappedBy="mother")
private List<Student> childrenMother = new ArrayList<>();
@OneToMany(mappedBy="father")
private List<Student> childrenFather = new ArrayList<>();
关于java - 有没有办法让两个mappedBy ="something"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60578216/