ModelMapper – failed to convert org.hibernate.collection.internal.PersistentBag to java.util.ArrayList

huangapple go评论70阅读模式
英文:

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 = &quot;employee&quot;, cascade = CascadeType.ALL, orphanRemoval = true)
private List&lt;Phones&gt; phones = new ArrayList&lt;&gt;();

My second entity:

@ManyToOne
@JoinColumn(name = &quot;employee_id&quot;)
private Employee employee;

My DTO which I want to have:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor(force = true)
public class EmployeeDTO {

@Singular
private final List&lt;PhoneDTO&gt; phones;

}

I am mapping this in this way:

        EmployeeDTO.EmployeeDTOBuilder employeeDTOBuilder 
= ObjectMapperUtils.map(employee, EmployeeDTO.employeeDTOBuilder.class);

I'm getting:

  1. 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&lt;PhoneEntity, PhoneDto&gt;
{
   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.

huangapple
  • 本文由 发表于 2020年9月3日 14:42:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63718182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定