我有两张桌子,一张是商人,另一张是产品。首先,我将添加商家详细信息,然后我必须选择商家并将这些商家分配给产品。问题是当我分配带有ID的商家详细信息时会抛出错误。
{
"name": "product A",
"quantityType": "kg",
"status": false,
"quantity": "30",
"createdBy": null,
"modifiedBy": null,
"createdDate": null,
"modifiedDate": null,
"merchantSet": [
{
"id":4,
"idNo": "XXXXXXXXXXV",
"idType": "NIC",
"emailId": "majutharan01@gmail.com",
"name": "aaaaaaaaa",
"contactNo": "0766601122",
"status": true,
"createdBy": null,
"modifiedBy": null,
"createdDate": null,
"modifiedDate": null
}
],
"category": {
"id": 1
}
}
这是错误:
{
"timestamp": 1554450979205,
"status": 500,
"error": "Internal Server Error",
"message": "detached entity passed to persist: com.xxxxx.xxxxxxx.model.Merchant; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.digiratina.islandgreen.model.Merchant",
"path": "/product-service/save-product"
}
当我添加不带id的商家详细信息时,它将保存,但会为商家创建新的id并更新中间表,但是我的情况是我必须将现有的商家id添加到中间表。
商人桌...
@Getter
@Setter
@Entity
@Table(name = "merchant")
public class Merchant {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // Auto generate Id...
@Column(name = "id")
private Long id;
@Column(name = "id_no")
private String idNo;
@Column(name = "id_type")
private String idType;
@Column(name = "email_id")
private String emailId;
@Column(name = "name")
private String name;
@Column(name = "contact_no")
private String contactNo;
@Column(name = "status")
private boolean status;
@Column(name = "created_by")
private String createdBy;
@Column(name = "modified_by")
private String modifiedBy;
@Column(name = "created_date")
private Date createdDate;
@Column(name = "modifiedDate")
private Date modifiedDate;
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, mappedBy="merchantSet")
@Getter(onMethod = @__( @JsonIgnore))
@Setter
private Set<Product> productSet = new HashSet<Product>();
产品表...
@Getter
@Setter
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // Auto generate Id...
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "quantity_type")
private String quantityType;
@Column(name = "status")
private boolean status;
@Column(name = "quantity")
private String quantity;
@Column(name = "created_by")
private String createdBy;
@Column(name = "modified_by")
private String modifiedBy;
@Column(name = "created_date")
private Date createdDate;
@Column(name = "modifiedDate")
private Date modifiedDate;
// @ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, mappedBy="productSet")
@ManyToMany(cascade = {CascadeType.ALL, CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name = "product_merchant",
joinColumns = {@JoinColumn(name = "product_id")},
inverseJoinColumns = {@JoinColumn(name = "merchant_id")})
private Set<Merchant> merchantSet = new HashSet<Merchant>();
@ManyToOne(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinColumn(name = "category_id", nullable = false)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private Category category;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
@Getter(onMethod = @__( @JsonIgnore ))
@Setter
private Set<Batch> batches = new HashSet<Batch>(
0);
public Product() {
}
public Product(Long id, String name, String quantityType, boolean status, String quantity, String createdBy, String modifiedBy, Date createdDate, Date modifiedDate) {
this.id = id;
this.name = name;
this.quantityType = quantityType;
this.status = status;
this.quantity = quantity;
this.createdBy = createdBy;
this.modifiedBy = modifiedBy;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate;
}
public Product(String name, String quantityType, boolean status, String quantity, String createdBy, String modifiedBy, Date createdDate, Date modifiedDate, Set<Merchant> merchantSet, Category category) {
this.name = name;
this.quantityType = quantityType;
this.status = status;
this.quantity = quantity;
this.createdBy = createdBy;
this.modifiedBy = modifiedBy;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate;
this.merchantSet = merchantSet;
this.category = category;
}
} ```
This is my service...
``` public Product addProduct(Product product) {
if (product.getCategory().getId() != null && product.getMerchantSet() != null && product.getCategory().getId() > 0) {
product.setCategory(categoryRepository.findCategoryById(product.getCategory().getId()));
product.setMerchantSet(product.getMerchantSet());
Product saveProduct = productRepository.save(product);
return saveProduct;
}
return null;
}