@OneToMany映射JPA中的父ID为null [英] Parent Id null in @OneToMany mapping JPA

查看:504
本文介绍了@OneToMany映射JPA中的父ID为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using javax.persistence.OneToMany relationship in a Parent Child relationship. The parent Id is coming as null, I have read through all the related post in Stackoverflow but not getting any clue what I am missing. All the Corresponding PKs are getting populated in both Parent & Child tables as per the Sequence Provided, but the FK is set to null in Child table

父类:

@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<>();

儿童课程:

@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;

服务类别:

 diversityTemplateRepository.save(diversityTemplate);

示例json

{
  "diversityTemplateId": 0,
  "attributes": [{
    "diversityTemplateId": 0,
    "diversityTemplateAttributeId": 0,
  }, {
    "diversityTemplateId": 0,
    "diversityTemplateAttributeId": 0,
  }]
}

请建议。

推荐答案

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

Usually empty FK columns are coming from setting only one side of the relation.

我想你有以下

DiversityTemplate diversityTemplate = ...
diversityTemplate.getAttributes().add(...)
...
diversityTemplateRepository.save(diversityTemplate);

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

This is wrong because the DiversityTemplateAttribute doesn't know about the parent, only the parent know about his children.

解决这个问题很简单,你必须在孩子中设置父引用。

Solving this is easy, you have to set the parent reference in the child.

diversityTemplateAttribute.setDiversityTemplate(diversityTemplate);

或者您可以将此逻辑放入 DiversityTemplate 自动将属性添加到List +中设置反向引用。

Or you can put this logic into a method within the DiversityTemplate which automatically adds the attribute into the List + sets the backreference.

这篇关于@OneToMany映射JPA中的父ID为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆