【发布时间】: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