【问题标题】:New child entity is getting created while Updating Parent in OneToMany JPA在 OneToMany JPA 中更新父级时正在创建新的子实体
【发布时间】:2017-09-10 06:49:04
【问题描述】:

您好,我有一个父子表,如下所示。有以下问题。 我正在使用 Spring Data Repository (org.springframework.data.repository)

问题 1 虽然我坚持父子条目也被插入,但是当我尝试更新父项(父项和子项中都存在新的更改)时,新的子项正在使用更新的数据插入到子表中而不是更新旧的子数据。

问题 2 我在这里打补丁,数据来自 UI 作为 json,我有一些审计跟踪字段,如 createdBy、createdTimestamp、updatedBy、updatedTimestamp。这些字段分别在创建和更新操作中填充到后端服务中。现在在更新操作中,我的 dto 没有 createdBy 和 createdTimestamp 的任何值,所以在 DB 中它被设置为 null。我在这里很困惑,我正在使用补丁调用,那么它应该保留旧值对吗?

如果我错过了请提出建议

家长:

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

@Column(name = "LABEL")
private String label;

@Column(name = "RELATIONSHIP_TYPE")
private String relationshipType;

@Column(name = "CREATED_BY")
private String createdBy;

@Column(name = "CREATED_TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
private Date createdTimestamp;

@Column(name = "UPDATED_BY")
private String updatedBy;

@Column(name = "UPDATED_TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedTimestamp;

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

/**
 * @return the attributes
 */
public List<DiversityTemplateAttribute> getAttributes() {
    return attributes;
}

/**
 * @param attributes the attributes to set
 */
public void setAttributes(List<DiversityTemplateAttribute> attributes) {
    for (DiversityTemplateAttribute diversityTemplateAttribute : attributes) {
        diversityTemplateAttribute.setDiversityTemplate(this);
    }
    this.attributes = attributes;
}

孩子:

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

@Column(name = "AA")
private String aa;

@Column(name = "BB")
private String bb;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DIVERSITY_TEMPLATE_ID", referencedColumnName = "DIVERSITY_TEMPLATE_ID")
private DiversityTemplate diversityTemplate;

更新 JSON 示例

{
"diversityTemplateId": 681,
"label": "SAMPLE_LABEL_463_UPDATED",
"relationshipType": "Married",
"attributes": [{
    "diversityTemplateId": 681,
    "diversityTemplateAttributeId": 3006,
    "aa": "AA",
    "bb": "BB Updated",
}, {
    "diversityTemplateId": 681,
    "diversityTemplateAttributeId": 3006,
    "aa": "aa Updated",
    "bb": "bb"
}
]

}

服务层:

DiversityTemplate updatedEntity = diversityTemplateRepository.save(diversityTemplate);

问题 3 如果将实体对象(当我从 GET/CREATE 操作获取它时)映射回 DTO 对象,我无法在子对象中设置 FK id,因此作为一种解决方法,我正在遍历对象的子列表和设置手动在子 DTO 中的父 pk,有没有更好的方法来做到这一点。我在子实体类中添加了另一个瞬态列,其 pk 列名称与父类中的相同,但它的值也为零,有没有更好的方法?请在下面找到我的解决方法。

DiversityTemplate updatedEntity = diversityTemplateRepository.save(diversityTemplate);

    List<DiversityTemplateAttributeDTO> attrbiteList = new ArrayList<>();
    for (DiversityTemplateAttribute attribute : updatedEntity.getAttributes()) {
        DiversityTemplateAttributeDTO attributeDTO = resourceMapper
                .convertToDiversityTemplateAttributeDTO(attribute);
        attributeDTO.setDiversityTemplateId(updatedEntity.getDiversityTemplateId());
        attrbiteList.add(attributeDTO);
    }

    DiversityTemplateDTO updatedDiversityTemplateDTO = resourceMapper.convertToDiversityTemplateDTO(updatedEntity);
    diversityTemplateDTO.setAttributes(attrbiteList);

请推荐

【问题讨论】:

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


    【解决方案1】:

    我和你有同样的问题。

    答案 1:我最终做的是在 @OneToMany 上将 orphanRemoval 标志设置为 true。如果您这样做,请注意您不能再将该子列表设置为等于新列表,否则它将通过并出错。您必须根据要删除和添加的内容来删除和附加。

    答案 2:您缺少父类顶部的 @EntityListeners(AuditingEntityListener.class) 但以防万一它不起作用,这是我用来让它工作的链接https://dzone.com/articles/spring-data-jpa-auditing-automatically-the-good-stuff

    答案 3:没有设置 id 的原因是因为在 java 代码中您必须在子级中设置对父级的引用。 即 child.setDiversityTemplate(parent); 一旦你这样做了,它就会在表格中正确地映射它,你就可以毫无问题地检索它。

    【讨论】:

      猜你喜欢
      • 2020-04-19
      • 1970-01-01
      • 2016-11-16
      • 2020-11-09
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      相关资源
      最近更新 更多