java - 有没有办法让两个mappedBy ="something"?

标签 java spring hibernate jpa

我是 SpringJPA 的新手。我有一个 parent classparent具有属性 @OneToMany(mappedBy="father") private List<Student> children = new ArrayList<>();

还有一个class Student与属性

      @ManyToOne
      private Parent father;

      @ManyToOne
      private Parent mother; 

当我想删除父亲时,它可以工作,但不能删除母亲。

我确实想保留这两个属性 fathermother并将它们都映射到 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/

相关文章:

java - 系统跳过 Hibernate 映射注释

hibernate - 如何在spring boot中获取postgres jsonb列的嵌套对象的值

java - 向 h :dataTable via AJAX with request-scoped bean without losing the row data 添加一行

java - ArrayIndexOutOfBoundsException : 1. 我的索引有什么问题? java

java - GUI 和边框布局

spring - 配置2个LDAP服务器

java - spring 不强制执行方法安全注释

java - 如何对 spring boot 微服务数据库应用进行负载均衡

java - 从数据库创建 Spring Bean

java - 不支持的操作异常 : The application must supply JDBC connections JPA Hibernate Spring JDBC