修补实体并获取重复项

时间:2019-05-30 06:43:42

标签: java spring hibernate spring-boot jpa

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

每当我编辑一个ObjectSpecification时,我们说“ length:1”,然后尝试在其中添加“ width:2”,最终结果是“ length:1,length:1,width: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 个答案:

答案 0 :(得分:1)

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

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

DimensionalCondition

3)确保至少在ObjectSpecification上将级联配置为List<DimensionalCondition> dimensionalConditions = new ArrayList<>();