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

每当我编辑一个具有“ length:1”的ObjectSpecification时,我都会尝试在其中添加“ 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)这是一个很大的更新列表,在进行任何修改之前,请确保合并ObjectSpecification使其与PersistenceContext对齐。

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

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


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

10-06 06:34