问题映射字段 ModelMapper
Issue mapping fields ModelMapper
我使用 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"
}
它returns我:
{
"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,名称 = topCategory ...
和 :
id = 2 - parent_id = 1 , category_key_id = "another generated key", name= sub
不幸的是,我只保留了 id、categorykeyid 和名称。
我从 CategoryDto 中删除了 id,我得到:1) Converter org.modelmapper.internal.converter.NumberConverter@348fc3d8 failed to convert java.lang.String to java.lang.Long.
我用"dirty"的方式解决了。
我只是更改了条目中的对象并添加了一个长 ID。
它给了我:
@Getter @Setter
public class CategoryRequestModel {
private Long id;
private String name;
private String parentCategoryKeyId;
}
我使用 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"
}
它returns我:
{
"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,名称 = topCategory ... 和 : id = 2 - parent_id = 1 , category_key_id = "another generated key", name= sub
不幸的是,我只保留了 id、categorykeyid 和名称。 我从 CategoryDto 中删除了 id,我得到:1) Converter org.modelmapper.internal.converter.NumberConverter@348fc3d8 failed to convert java.lang.String to java.lang.Long.
我用"dirty"的方式解决了。 我只是更改了条目中的对象并添加了一个长 ID。
它给了我:
@Getter @Setter
public class CategoryRequestModel {
private Long id;
private String name;
private String parentCategoryKeyId;
}