【问题标题】:@ManyToOne not filling the parent id field@ManyToOne 未填写父 id 字段
【发布时间】:2018-10-05 13:39:14
【问题描述】:

我在父实体和子实体之间存在多对一的双向关系。问题是,当我坚持孩子时,parent_id 没有坚持。其他字段都很好,但parent_id 留在NULL。如果有帮助的话,我正在使用 Spring Data JPA 和 Hibernate 和 mapstruct 在实体和 dto 之间进行转换。

java文件如下:

@Entity
@Table(name = "Parent")
public class ParentEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;


    @OneToMany (fetch = FetchType.LAZY, mappedBy="parent_entity", cascade = { CascadeType.ALL })
    private List<Child> children;

}


@Entity
@Table(name = "Child")
public class ChildEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne(optional = true)
    @JoinColumn(nullable = true, name = "parent_entity_id", insertable = false, updatable = false)
    private Parent parent_entity;
}

我已经尝试了 SO 的一堆答案,但现在无济于事。

【问题讨论】:

    标签: java spring-data-jpa one-to-many many-to-one


    【解决方案1】:

    由于您使用的是双向关系,因此您现在有责任为您的孩子设置父母。并删除insertable = false。

    【讨论】:

    • 谢谢!就是这样。 insertable = false 和作业...虽然我确定我之前曾尝试过将这两者放在一起,但我可能错过了在试验过程中解决的另一个问题。
    【解决方案2】:

    据我所知。您必须在父母和孩子中相互添加。仅将孩子添加到列表中似乎不起作用。

    试一试,看看是否能解决问题。

    【讨论】:

      猜你喜欢
      • 2017-12-15
      • 1970-01-01
      • 2019-01-04
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 2019-09-02
      相关资源
      最近更新 更多