Reputation: 69
I have the two entities with a manyToMany relationship:
@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<>();
}
In the database I have a join Table product_category that hold the product_id and category_id. my question is how to add element to the joinTable product_category? is it possible to create a Repository even if we don't have an entities??
I tried this with my 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);
}
}
but I got this:
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
Upvotes: 0
Views: 181
Reputation: 2570
You have relationship consistency issue. you are adding a category to a product but not adding product into category
add this method into your ProductEntity class:
public void addCategory(CategoryEntity category) {
this.getCategories().add(category);
category.getProducts().add(this);
}
and use this method to add category into product.
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());
Upvotes: 1