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