繁体   English   中英

Spring 启动,@ManyToMany,为加入表创建存储库

[英]Spring boot, @ManyToMany, create a repository for the join Table

我有两个具有多对多关系的实体:

@Entity
@Table(name="categories")
public class CategoryEntity implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private int categoryId;

    @Column(name="name")
    private String CategoryName;


    @ManyToMany(mappedBy = "categories")
    private List<ProductEntity> products = new ArrayList<ProductEntity>();
}

@Entity
@Table(name="products")
public class ProductEntity implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private Integer productId;

    @Column(name="name")
    private String productName;

    @Column(name="description")
    private String description;

    @Column(name="price")
    private Float price;

    @Column(name="rating")
    private Float rating;

    @Column(name="image")
    private String image;

    @Column(name="quantity")
    private Integer quantity;

    @ManyToMany(cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
    })
    @JoinTable(name = "product_category",
        joinColumns = {@JoinColumn(name = "product_id")},
        inverseJoinColumns = {@JoinColumn(name = "category_id")}
    )
    private List<CategoryEntity> categories = new ArrayList<>();
}

在数据库中,我有一个包含 product_id 和 category_id 的联接表 product_category。 我的问题是如何将元素添加到 joinTable product_category? 即使我们没有实体也可以创建存储库吗?

我用我的 controller 试过这个:

public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductReviewService reviewService;
    @Autowired
    private ProductReviewMapper reviewMapper;

    @PostMapping("/products")
    public ResponseEntity<ProductDto> createProduct(@RequestBody ProductDto productDto) {
        ProductEntity productEntity=productMapper.dtoToEntity(productDto);
        for(CategoryDto categoryDto:productDto.getCategories()){
            CategoryEntity categoryEntity=categoryMapper.dtoToEntity(categoryDto);
            productEntity.getCategories().add(categoryEntity);
        }
        productEntity=productService.saveProduct(productEntity);
        productDto.setProductId(productEntity.getProductId());

        return ResponseEntity.created(null).body(productDto);
    }
}

但我得到了这个:


org.hibernate.PersistentObjectException: detached entity passed to persist: com.be.ec.entities.CategoryEntity
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:127) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
    at 

你有关系一致性问题。 您正在将类别添加到产品中,但未将产品添加到类别中

将此方法添加到您的 ProductEntity class 中:

 public void addCategory(CategoryEntity category) {
        this.getCategories().add(category);
        category.getProducts().add(this);
    }

并使用此方法将类别添加到产品中。

ProductEntity productEntity=productMapper.dtoToEntity(productDto);
for(CategoryDto categoryDto:productDto.getCategories()){
    CategoryEntity categoryEntity=categoryMapper.dtoToEntity(categoryDto);
    productEntity.addCategory(categoryEntity); //changed line
   }
productEntity=productService.saveProduct(productEntity);
productDto.setProductId(productEntity.getProductId());

暂无
暂无

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

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