【问题标题】:Why does Orika map unused fields of nested List as null?为什么 Orika 将嵌套列表的未使用字段映射为 null?
【发布时间】:2014-11-14 11:31:33
【问题描述】:

两个实体:CustomerCustomerAddress - CustomerList<CustomerAddress>

public class Customer implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String name;

    //bi-directional many-to-one association to CustomerAddress
    @OneToMany(mappedBy="customer", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    private List<CustomerAddress> customeraddresses;

    private String identifier;

    private String aliasId;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(updatable=false)
    private Date createdDateTime;

    // Getters and Setters
}
public class CustomerAddress implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String line;

    //bi-directional many-to-one association to Customer
    @ManyToOne
    @JoinColumn(name="customerId")
    private Customer customer;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(updatable=false)
    private Date createdDateTime;

    private String gisLocation;

    // Getters and Setters
}

相应的 DTO:

public class CustomerDTO {
    private int id;
    private String name;
    private String aliasId;
    private List<CustomerAddressDTO> customeraddresses;
    // Getters and Setters
}
public class CustomerAddressDTO {
    private int id;
    private String line;
    // Getters and Setters
}

注意:实体中存在未在 DTO 中使用的未使用/额外字段

来自更新方法的片段:

Customer oldCustomer = customerDao.getCustomerById(dto.getId());
mapper.map(dto, oldCustomer);

现在,在这个映射器调用之后,List 中未使用的字段被设置为 null(这是不对的)

确保 Orika 保留嵌套列表中未使用的字段的正确方法是什么?


(如果需要,请提供其他信息):

oldCustomer before mapping: Customer [id=2, name=Customer1, customeraddresses=[CustomerAddress [id=7, line=ABCD**, createdDateTime=Sat Jan 01 00:00:00 IST 2000, gisLocation=1], CustomerAddress [id=13, line=0000, createdDateTime=Mon Jan 01 00:00:00 IST 2001, gisLocation=2]], identifier=id1, aliasId=1234, createdDateTime=Tue Dec 12 00:00:00 IST 2000]
dto used for mapping: CustomerDTO [id=2, name=Customer1, aliasId=1234, customeraddresses=[CustomerAddressDTO [id=7, line=ABCD**], CustomerAddressDTO [id=13, line=0000]]]
oldCustomer after mapping: Customer [id=2, name=Customer1, customeraddresses=[CustomerAddress [id=7, line=ABCD**, createdDateTime=null, gisLocation=null], CustomerAddress [id=13, line=0000, createdDateTime=null, gisLocation=null]], identifier=id1, aliasId=1234, createdDateTime=Tue Dec 12 00:00:00 IST 2000]

(注意:createdDateTimegisLocation 设置为 null - 这些应保留原始值)

【问题讨论】:

标签: java orika


【解决方案1】:

在 Orika 这是默认行为,您也可以通过提供合并两个集合的映射器来自定义此行为。项目的测试代码中有例子。

请查看此示例:CustomMerge

【讨论】:

  • 感谢您的回复。查看示例,似乎我们需要为每个实体-dto 对提供一个自定义映射器。这似乎太费力了。最好有一个通用的内置合并算法。
  • 如果您有 getId 接口(例如),则无需为每个 DTO/实体对执行此操作。顺便说一句,没有通用合并,这取决于您的策略,如果您想要退出替换、累积或协调......您描述的策略更像是一种协调,对于协调,您需要一个唯一标识符来匹配 dto 与实体您需要为此进行复制。这更像是一个应用程序问题而不是通用问题,因为它增加了开发人员应该意识到的成本。我们试图将这些选择排除在图书馆之外,但也许我们可以提供一些提高生产力的策略
猜你喜欢
  • 2018-04-21
  • 2016-08-21
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2015-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多