修补实体并获取重复项

 收藏

I'm having problems with editing an entity. I have ObjectSpecifications that have different conditions. The problem I have has to do with the 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) This is a big update list there, make sure you merge your ObjectSpecification so that it is aligned with the PersistenceContext before you do any amendments.

    2) You do not take under consideration the existing DimensionalCondition that are linked to ObjectSpecification. You always create a new one:

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

    3) Make sure you have your cascading configured to {PERSIST, MERGE} at least on @OneToMany List<DimensionalCondition>.