Код: Выделить всё
@Entity
class CategoryEntity {
private Long id;
private String name;
}
@Entity
class ProductEntity {
private Long id;
private String name;
private CategoryEntity category;
}
Код: Выделить всё
record ProductDto(String name, String categoryName) { }
Код: Выделить всё
public ProductEntity processProduct(ProductDto productDto) {
// find and check it exists
CategoryEntity categoryEntity = categoryService.findCategoryByName(productDto.categoryName());
ProductEntity productEntity = productMapper.toEntity(productDto);
productEntity.setCategory(categoryEntity);
// do some processing
// ...
return productEntity;
}
Я попробовал передать категорию экземпляр объекта в качестве параметра:
Код: Выделить всё
@Mapper
interface ProductMapper {
@Mapping(target = "category", source = "categoryEntity")
ProductEntity toEntity(ProductDto dto, CategoryEntity categoryEntity);
}
, что совершенно неверно.
Это проект Quarkus с MapStruct 1.5.5.
Подробнее здесь: https://stackoverflow.com/questions/791 ... ner-entity
Мобильная версия