I have three classes of entity(order,customer,sallers) and three classes of Dto. Me need to use Orika for mapping objects from entity classes to Dto. One of this(order) consist of data and links to another Dto objects. How i can map order entity to orderDto with orika uses mapperFactory or different sight mapper in orika. At the moment i have a two mapperFactory for transfer Sellers and Customers to Dto maybe can i use them?
(hide not important information)
Orders
@Entity
@Table(name="Orders")
@Data
public class Orders {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "seller_id", nullable = false)
private Sellers seller;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "customer_id", nullable = false)
private Customers customer;
}
Customers
public class Customers {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "order_id")
@JsonIgnoreProperties
private Set<Orders> orders;
}
Sellers
public class Sellers{
@OneToMany(fetch = FetchType.LAZY, mappedBy = "order_id")
private Set<Orders> orders;}
OrdersDto
public class OrdersDto
{
private SellersDto seller;
private CustomersDto customer;
}
In my case i used this solution:
You need to change your DTO class. Just change inheritance links from dto to Entity.
public class OrdersDto
{
private Sellers seller // old: SellersDto seller;
private Customers customer; // old: CustomersDto customer;
}
Names of fields in your entity and dto class should be equals by name. For correct links on database use @JoinColumn and @Column. And when you use:
mapperFactory.classMap(OrdersDto.class, Orders.class).byDefault().register();
Your inner classes (for example, Sellers and Buyers for me) are automatically rendered by fields because their fields are also rendered "by default".