I have an Order entity which looks like this
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@Enumerated(EnumType.STRING)
@Column(name = "order_status")
private OrderStatus status;
@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.DETACH, CascadeType.REFRESH})
@JoinTable(name = "order_product"
,joinColumns = @JoinColumn(name = "order_id")
,inverseJoinColumns = @JoinColumn(name = "product_id"))
private List<Product> productList;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@Column(name = "ordered_at")
private LocalDateTime orderTime;
@OneToOne
@JoinTable(name = "order_payment"
,joinColumns = @JoinColumn(name = "order_id",referencedColumnName = "id")
,inverseJoinColumns = @JoinColumn(name = "payment_id", referencedColumnName = "id"))
private Payment payment;
@ManyToOne
@JoinColumn(name = "shop_id")
private Shop shop;
...
contsructor getter and setters
}
OrderPostDto
public class OrderPostDto {
private int id;
private OrderStatus status;
private int userId;
private LocalDateTime orderTime;
private List<Integer> productIds;
private int shopId;
...
constructor getter and setters
}
MapStruct OrderMapper
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.CONSTRUCTOR, uses = {ProductService.class, ShopService.class, UserService.class})
public interface OrderMapper {
OrderMapper INSTANCE = Mappers.getMapper(OrderMapper.class);
OrderDto orderToDto(Order order);
@Mapping(source = "userId", target = "user")
@Mapping(source = "productIds", target = "productList")
@Mapping(source = "shopId", target = "shop")
Order dtoToOrder(OrderPostDto dto);
}
As you can see the OrderDto accepts Product ids as Integers and OrderMapper should Map them to the object from database table of products. But it generates code like this:
protected List<Product> integerListToProductList(List<Integer> list) {
if ( list == null ) {
return null;
}
List<Product> list1 = productService.getAllProducts();
for ( Integer integer : list ) {
list1.add( productService.getProductById( integer.intValue() ) );
}
return list1;
}
But for some reason it creates list1 which contains all the items from database List<Product> list1 = productService.getAllProducts();
But I need to achieve this behaviour List<Product> list1 = new ArrayList<>(list.size());
How do I make it generate this way?
CodePudding user response:
I've solved my issue, I just defined mapper as an abstract class with implementation of this particular method. So it will look like this:
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.CONSTRUCTOR, uses = {ProductService.class, ShopService.class, UserService.class})
public abstract class OrderMapper {
@Autowired
protected ProductService productService;
public abstract OrderDto orderToDto(Order order);
@Mapping(source = "userId", target = "user")
@Mapping(source = "productIds", target = "productList")
@Mapping(source = "shopId", target = "shop")
public abstract Order dtoToOrder(OrderPostDto dto);
public List<Product> integerListToProductList(List<Integer> list) {
if ( list == null ) {
return null;
}
List<Product> list1 = new ArrayList<>(list.size());
for ( Integer integer : list ) {
list1.add( productService.getProductById( integer.intValue() ) );
}
return list1;
}
}