【问题标题】:ModelMapper Set attribute of nested objectModelMapper 设置嵌套对象的属性
【发布时间】:2020-10-19 17:17:28
【问题描述】:

有没有办法配置模型映射器以自动将父 ID 映射到嵌套子对象中的父 ID?

父实体

@Entity
@Table(name = "parent")
public class ParentEntity {

    @Id
    @Column(name = "id")
    private Long id;

    @Basic
    @Column(name = "name")
    private String name;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    private List<ChildEntity> child;

子实体

@Entity
@Table(name = "child")
public class ChildEntity {

    @Id
    @Column(name = "id")
    private Long id;

    @Basic
    @Column(name = "parent_id")
    private Long parentId;

    @Basic
    @Column(name = "name")
    private String name;

父 DTO

public class ParentDto {
    
    private Long id;
    private String name;
    private List<ChildDto> children;

子 DTO


public class ChildDto {

    private Long id;
    private Long parentId;
    private String name;

目前我使用模型映射器,它的转换在大多数情况下都有效,它创建了一个包含 ChildEntity 对象列表的 ParentEntity 对象。现在我正在寻找一种方法来使用模型映射器填充每个 ChildEntity 中的“parentId”字段。提前致谢!

/*
{
  id: 1,
  name: "dad",
  children: [
    {
      id:10,
      name: "child",
    },
    {
      id:20,
      name: "child2",
    }
  ]
}
*/
modelMapper.map(parentDto, ParentEntity.class)

【问题讨论】:

  • parent_id 中的 ChildEntity 是否没有引用 ParentEntity .id?如果是这样并且来自db,为什么需要自定义方法?
  • @Aman parent_id in ChildEntity 正在引用 ParentEntity.id,但是当我使用 modelMapper.map 时,它不会使用父 ID 填充 ChildEntities 列表。我猜是因为字段名称不同(id vs parent_id)
  • 不,他们不是。我的意思是,如果ChildEntity.parentId 像你说的那样来自数据库,映射应该不是问题。
  • @Aman 当我尝试将 parentDto 映射到 ParentEntity 时,嵌套的 ChildEntities 的 parentId 为空。转换有效,但我正在尝试对其进行配置,以便嵌套的 ChildEntities 使用 parentEntity 的 id 自动填充 parentId。我希望我是清楚的。
  • 正确的方法是修复父子之间断开的链接。您可以编写 converter 否则,它会为您提供映射上下文,以便您可以根据需要自定义映射。但是,随之而来的是不必要和无用的并发症。 modelmapper.org/user-manual/converters

标签: java modelmapper


【解决方案1】:

挑战在于,当映射ChildDto 时,您必须有权访问具有List children 的ParentDto,其中添加了要映射的ChildDto。否则映射器不知道该 id。

幸运的是,您可以在org.modelmapper.Converter 的帮助下访问ParentDto。像这样实现这个接口:

public Converter<ChildDto, ChildEntity> converter = new Converter<>() {
    private final ModelMapper mm = new ModelMapper();
    @Override
    public ChildEntity convert(MappingContext<ChildDto, ChildEntity> context) {
        // map it first normally
        ChildEntity ce = mm.map(context.getSource(), ChildEntity.class);
        try {
            // 1st parent is the List 2nfd is the ParentDto
            ParentDto parentDto = (ParentDto)context.getParent().getParent().getSource();
            ce.setParentId(parentDto.getId());
        } catch (Exception e) {
            // well, maybe it was not ParentDto that expected to be the grandparent.
        }
        return ce;
    }
};

然后像这样使用它:

ModelMapper mm = new ModelMapper();
mm.addConverter(converter);

没有parentId 的ChildDto 仍应映射到带有parentId 的ChildEntity。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多