@OneToMany 映射 JPA 中的父 ID 为空

Asi*_*lla 5 java persistence hibernate jpa one-to-many

我在父子关系中使用 javax.persistence.OneToMany 关系。父 ID 为空,我已经阅读了 Stackoverflow 中的所有相关帖子,但没有得到任何线索我遗漏了什么。 根据提供的序列,所有相应的 PK 都填充在父和子表中,但 FK 在子表中设置为空

家长班:

@Entity
@Table(name = "DIVERSITY_TEMPLATE")
public class DiversityTemplate implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "DIVERSITY_TEMPLATE_ID", sequenceName = "DIVERSITY_TEMPLATE_ID", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DIVERSITY_TEMPLATE_ID")
    @Column(name = "DIVERSITY_TEMPLATE_ID")
    private Integer diversityTemplateId;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "diversityTemplate", fetch = FetchType.LAZY)
    private List<DiversityTemplateAttribute> attributes = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)

儿童班:

@Entity
@Table(name = "DIVERSITY_TEMPLATE_ATTRIBUTE")
@TypeName("DiversityTemplateAttribute")
public class DiversityTemplateAttribute implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "DIVERSITY_TEMPLATE_ATTR_ID", sequenceName = "DIVERSITY_TEMPLATE_ATTR_ID", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DIVERSITY_TEMPLATE_ATTR_ID")
    @Column(name = "DIVERSITY_TEMPLATE_ATTR_ID")
    private Integer diversityTemplateAttributeId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "DIVERSITY_TEMPLATE_ID", nullable=false, referencedColumnName = "DIVERSITY_TEMPLATE_ID")
    private DiversityTemplate diversityTemplate;
Run Code Online (Sandbox Code Playgroud)

服务等级:

 diversityTemplateRepository.save(diversityTemplate);
Run Code Online (Sandbox Code Playgroud)

示例 json

{
  "diversityTemplateId": 0,
  "attributes": [{
    "diversityTemplateId": 0,
    "diversityTemplateAttributeId": 0,
  }, {
    "diversityTemplateId": 0,
    "diversityTemplateAttributeId": 0,
  }]
}
Run Code Online (Sandbox Code Playgroud)

请建议。

gal*_*ics 8

通常空 FK 列仅来自设置关系的一侧。

我想你有以下几点

DiversityTemplate diversityTemplate = ...
diversityTemplate.getAttributes().add(...)
...
diversityTemplateRepository.save(diversityTemplate);
Run Code Online (Sandbox Code Playgroud)

这是错误的,因为DiversityTemplateAttribute他不知道父母,只有父母知道他的孩子。

解决这个问题很容易,您必须在子项中设置父引用。

diversityTemplateAttribute.setDiversityTemplate(diversityTemplate);
Run Code Online (Sandbox Code Playgroud)

或者您可以将此逻辑放入方法中,DiversityTemplate该方法会自动将属性添加到列表中并设置反向引用。

  • 感谢@galovics,**现在可以使用**,我在父类中添加了反向引用,如下所示,非常感谢您的建议。`public void setAttributes(List&lt;DiversityTemplateAttribute&gt; attributes) { for (DiversityTemplateAttributediversTemplateAttribute : attributes) {diversityTemplateAttribute.setDiversityTemplate(this); } this.attributes = 属性;}` (2认同)

小智 6

我知道为时已晚,但您也可以...

家长班:

@Entity
@Table(name = "DIVERSITY_TEMPLATE")
public class DiversityTemplate implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "DIVERSITY_TEMPLATE_ID", sequenceName = "DIVERSITY_TEMPLATE_ID", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DIVERSITY_TEMPLATE_ID")
    @Column(name = "DIVERSITY_TEMPLATE_ID")
    private Integer diversityTemplateId;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "DIVERSITY_TEMPLATE_ID")
    private List<DiversityTemplateAttribute> attributes = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)

儿童班:

@Entity
@Table(name = "DIVERSITY_TEMPLATE_ATTRIBUTE")
@TypeName("DiversityTemplateAttribute")
public class DiversityTemplateAttribute implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "DIVERSITY_TEMPLATE_ATTR_ID", sequenceName = "DIVERSITY_TEMPLATE_ATTR_ID", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DIVERSITY_TEMPLATE_ATTR_ID")
    @Column(name = "DIVERSITY_TEMPLATE_ATTR_ID")
    private Integer diversityTemplateAttributeId;

    @ManyToOne(fetch = FetchType.LAZY)
    private DiversityTemplate diversityTemplate;
Run Code Online (Sandbox Code Playgroud)

服务等级:

diversityTemplateRepository.save(diversityTemplate);
Run Code Online (Sandbox Code Playgroud)

这样您就无需为每个孩子都进行家长登记。它会自己做。你只需要保存父母就可以了。

快速步骤我所做的。

  1. 从父级中删除了mappedBy。
  2. 在 Parent 中添加了 @JoinCollumn 外键
  3. 从 Child 中删除了 @JoinCollumn。

希望能帮助到你。