【问题标题】:Issue mapping fields ModelMapper问题映射字段 ModelMapper
【发布时间】:2019-11-19 09:44:17
【问题描述】:

我使用 DTO 和 modelMapper 是为了不让某些字段可见。 我有一个可以有子类别的 CategoryEntity

public class CategoryEntity {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;

    @Column(length = 30, nullable = false)
    private String categoryKeyId;

    @Column(nullable = false)
    private String name;

    @ManyToOne(optional = true, fetch = FetchType.LAZY)
    @JoinColumn(name="parent_id", nullable=true)
    private CategoryEntity parentCategory;

    // allow to delete also subcategories
    @OneToMany(mappedBy="parentCategory", cascade = CascadeType.ALL)
    private List<CategoryEntity> subCategories;
}

当我创建一个类别时,我使用一个模型:

@Getter @Setter
public class CategoryRequestModel {
    private String name;
    private String parentCategoryKeyId;
}

在这个模型中,我希望 parentCategoryKeyId 与父类的 categoryKeyId 匹配。

例如,如果我创建一个“顶级”类别:

{
  "name": "topCategory"
} 

它返回我:

{
    "categoryKeyId": "jUcpO27Ch2YrT2zkLr488Q435F8AKS",
    "name": "topCategory",
    "subCategories": null
}

当我这样做时:

{
  "name": "sub",
  "parentCategoryKeyId": "jUcpO27Ch2YrT2zkLr488Q435F8AKS"
} 

在我的控制器中,我将其余对象传递给调用服务的 DTO 层:

public CategoryRestResponseModel createCategory(@RequestBody CategoryRequestModel categoryRequestModel) {
    CategoryRestResponseModel returnValue = new CategoryRestResponseModel();
    if( categoryRequestModel.getName().isEmpty())
        throw new NullPointerException(ErrorMessages.MISSING_REQUIRED_FIELDS.getErrorMessage());
    ModelMapper modelMapper = new ModelMapper();
    CategoryDto categoryDto = modelMapper.map(categoryRequestModel, CategoryDto.class);
    CategoryDto createdCategory = categoryService.createCategory(categoryDto);
    returnValue = modelMapper.map(createdCategory, CategoryRestResponseModel.class);
    return returnValue;
}

我的 CategoryDto 是一个基本的 POJO:

@Getter @Setter
public class CategoryDto implements Serializable {
    @Getter(AccessLevel.NONE)
    private static final long serialVersionUID = 1L;

    private String categoryKeyId;
    private String parentCategoryKeyId;
    private String name;
    private CategoryDto parentCategory;
    private List<CategoryDto> subCategories;
}

在我的服务中:

   public CategoryDto createCategory(CategoryDto categoryDto) {
        //1. Create an empty object to return
        System.out.println("Hello World");
        CategoryDto returnValue = new CategoryDto();
        System.out.println("CategoryDto: " + categoryDto);
        // check if category exists
        if (categoryRepository.findByName(categoryDto.getName()) != null)
            throw new ApplicationServiceException("Record already in Database");

        ModelMapper modelMapper = new ModelMapper();
        CategoryEntity categoryEntity = modelMapper.map(categoryDto, CategoryEntity.class);

        // Generate categoryKeyId
        String categoryKeyId = utils.generateCategoryKeyId(30);
        categoryEntity.setCategoryKeyId(categoryKeyId);
        System.out.println("categoryDto parentCategory: " + categoryDto.getParentCategory());
        System.out.println("CategoryDto: " + categoryDto);

        if(categoryDto.getParentCategoryKeyId() != null) {
            CategoryEntity parentCategory = categoryRepository.findByCategoryKeyId(categoryDto.getParentCategoryKeyId());
            categoryEntity.setParentCategory(parentCategory);
            System.out.println("CategoryEntity: " + categoryEntity);
            System.out.println("parentCategory: " + parentCategory);
        }

        CategoryEntity storedCategory = categoryRepository.save(categoryEntity);
        returnValue = modelMapper.map(storedCategory, CategoryDto.class);

        return returnValue;
    }

我的问题是我想保存子类别并检索与 categoryKeyId 匹配的 ID ...

在数据库中我的条目应该是这样的

我的第一个条目应该有: id = 1 - parent_id = null,category_key_id = jUcpO27Ch2YrT2zkLr488Q435F8AKS,name = topCategory ... 和 : id = 2 - parent_id = 1 , category_key_id = "另一个生成的密钥", name= sub

不幸的是,我只保留了 id、categoryke​​yid 和名称。 我从 CategoryDto 中删除了 id 并获得:1)转换器 org.modelmapper.internal.converter.NumberConverter@348fc3d8 无法将 java.lang.String 转换为 java.lang.Long。

【问题讨论】:

    标签: spring modelmapper


    【解决方案1】:

    我以“肮脏”的方式解决了它。 我只是在条目中更改了我的对象并添加了一个长 ID。

    它给了我:

    @Getter @Setter
    public class CategoryRequestModel {
        private Long id;
        private String name;
        private String parentCategoryKeyId;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多