我使用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相匹配。
例如,如果我创建了一个"top“类别:
{
"name": "topCategory"
} 它返回我:
{
"categoryKeyId": "jUcpO27Ch2YrT2zkLr488Q435F8AKS",
"name": "topCategory",
"subCategories": null
}当我这样做的时候:
{
"name": "sub",
"parentCategoryKeyId": "jUcpO27Ch2YrT2zkLr488Q435F8AKS"
} 在我的控制器中,我将rest对象传递给一个调用服务的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
不幸的是,我只是持久化了标识、类别键标识和名称。我从CategoryDto中删除了id,并获得: 1)转换器org.modelmapper.internal.converter.NumberConverter@348fc3d8无法将java.lang.String转换为java.lang.Long。
发布于 2019-11-19 18:43:25
我以一种“肮脏”的方式解决了这个问题。我只是更改了entry中的对象,并添加了一个长id。
它给了我:
@Getter @Setter
public class CategoryRequestModel {
private Long id;
private String name;
private String parentCategoryKeyId;
}https://stackoverflow.com/questions/58930929
复制相似问题