Two entities: Customer
, CustomerAddress
- Customer
has List<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
}
Corresponding DTOs:
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
}
Note: There are unused/extra fields in entity which are not used in DTO
Snippet from Update method:
Customer oldCustomer = customerDao.getCustomerById(dto.getId());
mapper.map(dto, oldCustomer);
Now, after this mapper call, the unused fields in List are set to null (which is not right)
What is the correct to ensure Orika preserves the unused fields on nested list?
(Additional information, if needed):
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]
(Note: createdDateTime
aand gisLocation
are set to null - these should retain original values)
In Orika this is the default behavior you can also customize this by providing a mapper that merge two collection. there is example in the test code of the project.
Please check this example : CustomMerge