首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >问题映射字段ModelMapper

问题映射字段ModelMapper
EN

Stack Overflow用户
提问于 2019-11-19 17:44:18
回答 1查看 196关注 0票数 1

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

代码语言:javascript
复制
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;
}

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

代码语言:javascript
复制
@Getter @Setter
public class CategoryRequestModel {
    private String name;
    private String parentCategoryKeyId;
}

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

例如,如果我创建了一个"top“类别:

代码语言:javascript
复制
{
  "name": "topCategory"
} 

它返回我:

代码语言:javascript
复制
{
    "categoryKeyId": "jUcpO27Ch2YrT2zkLr488Q435F8AKS",
    "name": "topCategory",
    "subCategories": null
}

当我这样做的时候:

代码语言:javascript
复制
{
  "name": "sub",
  "parentCategoryKeyId": "jUcpO27Ch2YrT2zkLr488Q435F8AKS"
} 

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
@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;
}

在我的服务中:

代码语言:javascript
复制
   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

不幸的是,我只是持久化了标识、类别键标识和名称。我从CategoryDto中删除了id,并获得: 1)转换器org.modelmapper.internal.converter.NumberConverter@348fc3d8无法将java.lang.String转换为java.lang.Long。

EN

回答 1

Stack Overflow用户

发布于 2019-11-19 18:43:25

我以一种“肮脏”的方式解决了这个问题。我只是更改了entry中的对象,并添加了一个长id。

它给了我:

代码语言:javascript
复制
@Getter @Setter
public class CategoryRequestModel {
    private Long id;
    private String name;
    private String parentCategoryKeyId;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58930929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档