Abstract class will throw a type error when using the MapStruct in Java 11. I'm currently using the Spring Boot framework.
I get a type error when using MapStruct. I use Mapstruct to convert between DTO and entity.
The error message is:
error: The return type Item is an abstract class or interface. Provide a non abstract / non interface result type or a factory method. Occured at 'E toEntity(D d)' in 'GenericMapper'.
public interface ItemReqMapper extends GenericMapper<ItemReqDto, Item> {
Item class is an abstract class. FYI, the reason I can't change the Item class to a normal class is because another class inherits from Item class.
I use ItemReqMapper inheriting GenericMapper
Can't I use an abstract class for entities in a generic mapper? Can't I use an abstract class in the following code? How can I solve this problem?
The code is as follows.
Item class
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "dtype")
@Getter
@NoArgsConstructor
@AllArgsConstructor
public abstract class Item {
@Id @GeneratedValue
@Column(name = "item_id")
private Long id;
private String name;
private Integer price;
private Integer stockQuantity;
public Item(String name, Integer price,Integer stockQuantity){
this.name = name;
this.price = price;
this.stockQuantity = stockQuantity;
}
}
ItremReqMapper interface
@Mapper(componentModel = "spring")
public interface ItemReqMapper extends GenericMapper<ItemReqDto, Item> {
}
GenericMapper interface
public interface GenericMapper <D, E> {
D toDto(E e);
E toEntity(D d);
List<D> toDtoList(List<E> entityList);
List<E> toEntityList(List<D> dtoList);
}
ItemServiceImpl class (I use itemReqMapper.toEntity() here)
@Transactional
public ItemResDto create(ItemReqDto reqDto){
Item item = itemRepository.save(**itemReqMapper.toEntity(reqDto)**);
return itemResMapper.toDto(item);
}
How can I solve this problem?