英文:
ModelMapper - failed to convert org.hibernate.collection.internal.PersistentBag to java.util.ArrayList
问题
// ModelMapper配置示例:
compile group: 'org.modelmapper', name: 'modelmapper', version: '2.3.8'
// 目前我有以下配置:
modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STANDARD)
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(Configuration.AccessLevel.PRIVATE);
// 我的实体类:
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Phones> phones = new ArrayList<>();
// 我的第二个实体类:
@ManyToOne
@JoinColumn(name = "employee_id")
private Employee employee;
// 我想要的DTO类:
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor(force = true)
public class EmployeeDTO {
@Singular
private final List<PhoneDTO> phones;
}
// 我是这样进行映射的:
EmployeeDTO.EmployeeDTOBuilder employeeDTOBuilder =
ObjectMapperUtils.map(employee, EmployeeDTO.EmployeeDTOBuilder.class);
// 我得到了以下错误:
1) 转换器 org.modelmapper.internal.converter.MergingCollectionConverter@969b4391 无法将 org.hibernate.collection.internal.PersistentBag 转换为 java.util.ArrayList。
英文:
How should ModelMapper configuration looks like?
compile group: 'org.modelmapper', name: 'modelmapper', version: '2.3.8'
Currently I have:
modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STANDARD)
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(Configuration.AccessLevel.PRIVATE);
My Entity has
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Phones> phones = new ArrayList<>();
My second entity:
@ManyToOne
@JoinColumn(name = "employee_id")
private Employee employee;
My DTO which I want to have:
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor(force = true)
public class EmployeeDTO {
@Singular
private final List<PhoneDTO> phones;
}
I am mapping this in this way:
EmployeeDTO.EmployeeDTOBuilder employeeDTOBuilder
= ObjectMapperUtils.map(employee, EmployeeDTO.employeeDTOBuilder.class);
I'm getting:
- Converter org.modelmapper.internal.converter.MergingCollectionConverter@969b4391 failed to convert org.hibernate.collection.internal.PersistentBag to java.util.ArrayList.
答案1
得分: 1
深入查看堆栈跟踪:最后的映射问题通常是可操作性更强的内容。
也许 PhoneDTO
无法映射?如果是这样,您需要创建一个转换器,并告诉模型映射器,像这样:
public class PhoneConverter extends AbstractConverter<PhoneEntity, PhoneDto>
{
private final StrictModelMapper strictModelMapper;
@Override
public PhoneDto convert(@NonNull final PhoneEntity source)
{
PhoneDto phoneDto = new PhoneDto();
strictModelMapper.map(source, phoneDto);
return phoneDto;
}
}
然后将其添加到 modelMapper
中。
英文:
Look a bit deeper in the stack trace: the last mapping problem is often something more actionable.
Maybe the PhoneDTO
was not mappable ? If so, you need to create a converter and tell model mapper about it, like so:
public class PhoneConverter extends AbstractConverter<PhoneEntity, PhoneDto>
{
private final StrictModelMapper strictModelMapper;
@Override
public PhoneDto convert(@NonNull final PhoneEntity source)
{
PhoneDto phoneDto = new PhoneDto();
strictModelMapper.map(source, phoneDto);
return phoneDto;
}
}
and add it to the modelMapper
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论