繁体   English   中英

通过 PostMapping 在数据库中添加新实体

[英]adding new entity in database through PostMapping

我有两个实体

@Entity
@Table(name = "categories")
public class Category {
    @Getter
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "category_id", unique = true, nullable = false)
    private long categoryId;

    @Getter @Setter
    @ManyToMany(mappedBy = "categories", cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
    })
    List<Product> products;

    @Getter @Setter
    @Column(name = "category_name", nullable = false, unique = true)
    private String categoryName;

@Entity
@Table(name = "products")
public class Product {
    @Getter
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "product_id", unique = true, nullable = false)
    private long productId;

    @Getter @Setter
    @Column(name = "price")
    private float price;

    @Getter @Setter
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "categories_product")
    private List<Category> categories;

    @Getter @Setter
    @Column(name = "product_code", unique = true, nullable = false)
    private String productCode;

    @Getter @Setter
    @Column(name = "product_name", nullable = false)
    private String productName;

    @Getter @Setter
    @Column(name = "description", nullable = false)
    private String description;

    @Getter @Setter
    @Column(name = "short_description", nullable = false)
    private String shortDescription;
}

我正在将 MapStruct 用于 DTO。 When I want to add new product through controller I get the following error: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.project.shop.models.Category.categoryName

据我了解,当我希望它使用数据库中已经存在的类别时,hibernate 会尝试创建一个新类别。

类别DTO:

@Getter
@Setter
public class CategoryDto {
    private long categoryId;
    private String categoryName;
    private boolean categoryActive;
}

产品DTO:

@Getter
@Setter
public class ProductDto {
    private String productName;
    private String productCode;
    private float price;
    private String shortDescription;
    private String description;

    private List<CategoryDto> categories;
}

类别映射器:

@Mapper(componentModel = "spring")
public interface CategoryMapper {
    CategoryDto toDto(Category category);
    List<CategoryDto> toDtos(List<Category> categories);
    List<Category> toModels(List<CategoryDto> categoryDtos);
    Category toModel(CategoryDto categoryDto);
}

产品映射器:

@Mapper(uses = {CategoryMapper.class},
        componentModel = "spring")
public interface ProductMapper {
    ProductDto toDto(Product product);
    List<ProductDto> toDtos(List<Product> products);
    List<Product> toModels(List<ProductDto> productDtos);
    Product toModel(ProductDto productDto);
}

Controller:

@PostMapping("/product")
public ResponseEntity<ProductDto> create(@RequestBody ProductDto productDto) {
    productService.save(productMapper.toModel(productDto));
    return ResponseEntity.status(HttpStatus.CREATED).body(productDto);
}

productService.save:

public void save(Product product) {
    productRepository.save(product);
}

基本上没那么容易。 我的建议(和我的实现)是你只传递categoryId和你的ProductDTO 在服务中,获取此 ID,通过存储库找到相应的类别,然后将产品的类别设置为此实体。 简单的例子:

    public ProductDTO addProduct(ProductDTO newDto) {

    Category category = categoryRepo.findById(newDto.getCategory().getId())
            .orElseThrow(// something);

    Product entity = modelMapper.map(newDto, Product.class); // This does the same thing as your mapper, You can also implement this in your project

    entity.setCategory(category );

    return modelMapper.map(productRepo.save(entity), ProductDTO.class); // This saves the entity and converts it to a DTO and returns it 
}

如果看身份,nullable = false 选项似乎被定义选项被定义。

@Entity
@Table(name = "categories")
public class Category {
    
    ....

    @Getter @Setter
    @Column(name = "category_name", nullable = false, unique = true)
    private String categoryName;

我认为最好先查找 CategoryDto 的 categoryName 列值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM