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

kzmpq1sx  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(516)

我有两个实体

@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。当我想通过控制器添加新产品时,出现以下错误: org.hibernate.PropertyValueException: not-null property references a null or transient value : com.project.shop.models.Category.categoryName 据我所知,当我希望hibernate使用数据库中已有的类别时,它会尝试创建一个新的类别。
类别:

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

产品目标:

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

    private List<CategoryDto> categories;
}

类别Map:

@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);
}

产品Map器:

@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);
}

控制器:

@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);
}
xiozqbni

xiozqbni1#

基本上没那么容易。我的建议(和我的实现)是你只通过 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 
}
3hvapo4f

3hvapo4f2#

如果查看标识,nullable=false选项似乎已定义option已定义。

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

    ....

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

我认为最好先查找categorydto的categoryname列值。

相关问题