我有两个具有manyToMany关系的实体:
@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_category,其中包含product_id和category_id。
我的问题是如何向joinTable product_category添加元素?即使我们没有实体,也可以创建存储库吗?
我尝试用我的控制器:
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类中:
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());
关于java - Spring 启动@ManyToMany,为联接表创建存储库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58936982/