gpt4 book ai didi

java - 修补实体并获取重复的项目

转载 作者:行者123 更新时间:2023-12-02 09:50:09 24 4
gpt4 key购买 nike

我在编辑实体时遇到问题。我有具有不同条件的 ObjectSpecifications。我遇到的问题与DimensionalConditions有关。

每当我编辑一个包含“长度:1”的 ObjectSpecification 时,我尝试向其中添加“宽度:2”,条件最终会变为“长度:1,长度:1,宽度:2”。

长话短说,之前存在的条件由于某种原因再次插入。

public ObjectSpecification modifySpecification(Long id, ObjectSpecificationRequest request) {
ObjectSpecification objectSpecification = this.getObjectSpecificationById(id);
return this.setSpecData(request, objectSpecification);
}

public ObjectSpecification createSpecification(ObjectSpecificationRequest request){
return this.setSpecData(request, new ObjectSpecification());
}

public void deleteSpecification(Long id) {
objectSpecificationRepository.delete(getObjectSpecificationById(id));
}

public ObjectSpecification setSpecData(ObjectSpecificationRequest request, ObjectSpecification objectSpecification) {
if (request.getName() != null) {
objectSpecification.setName(request.getName());
}

if (request.getDimensionalConditions() != null) {
List<DimensionalCondition> dimensionalConditions = new ArrayList<>();
for (DimensionalCondition condition:request.getDimensionalConditions()) {
DimensionalCondition dimensionalCondition = new DimensionalCondition();
dimensionalCondition.setConditionType(condition.getConditionType());
dimensionalCondition.setValue(condition.getValue());
dimensionalCondition.setObjectSpecification(objectSpecification);
dimensionalConditions.add(dimensionalCondition);
}
objectSpecification.setDimensionalConditions(dimensionalConditions);
}

if (request.getMechanisms() != null) {
objectSpecification.setMechanismConditions(request.getMechanisms());
}

if (request.getServices() != null) {
objectSpecification.setServiceConditions(request.getServices());
}

if (request.getWorkDetails() != null) {
List<WorkDetail> workDetails = new ArrayList<>();
for (WorkDetail workDetail:request.getWorkDetails()) {
workDetails.add(workDetailService.getWorkDetailById(workDetail.getId()));
}
objectSpecification.setWorkDetailConditions(workDetails);
}

return objectSpecificationRepository.save(objectSpecification);
}

最佳答案

1) 这是一个很大的更新列表,请确保合并您的 ObjectSpecification使其与 PersistenceContext 对齐在进行任何修改之前。

2) 您没有考虑现有的 DimensionalCondition链接到 ObjectSpecification 。您总是创建一个新的:

List<DimensionalCondition> dimensionalConditions = new ArrayList<>();

3) 确保您的级联配置为 {PERSIST, MERGE}至少在 @OneToMany List<DimensionalCondition>

关于java - 修补实体并获取重复的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56373049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com