Parent Id null in @OneToMany mapping JPA

霸气de小男生 提交于 2019-12-01 10:59:51

问题


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

Parent Class:

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

Child Class:

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

Service Class:

 diversityTemplateRepository.save(diversityTemplate);

Sample json

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

Please suggest.


回答1:


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

I imagine you have the following

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

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

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




回答2:


I know it's too late but you can also do...

Parent Class:

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

Child Class:

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

Service Class:

diversityTemplateRepository.save(diversityTemplate);

This way you DON'T NEED TO DO PARENT ENTRY IN EACH CHILD. it will do by itself. you just need to save parent that's it.

Quick steps what I did.

  1. Removed mappedBy from Parent.
  2. Added @JoinCollumn foreign key in Parent
  3. Removed @JoinCollumn from Child.

Hope it helps.



来源:https://stackoverflow.com/questions/43357413/parent-id-null-in-onetomany-mapping-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!