Issue
I have three entities namely
- product
- product details
- stock
- category reference is given below
when I try to get the details of product it works fine when I try to save it shows below error in the console
2020-08-12 13:17:22.279 WARN 18612 --- [nio-9002-exec-1] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.eMart.main.entity.Product]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'defaultReference': back reference type (java.util.List) not compatible with managed type (com.eMart.main.entity.Product)
My question is how to add the product into the database and how did I need to optimize my entities
Input
{
"skuId": "2",
"category": {
"categoryId": 2,
"categoryName": "food"
},
"description": "cow milk",
"stock": {
"stockId": 1,
"inventoryCount": 5,
"selfCount": 5
},
"productDetails": [
{
"productDetailId": 1,
"cost": 10.0,
"currency": "inr",
"expiryDate": "2020-08-11T18:30:00.000+00:00",
"supplierCode": 1
}
]
}
controller method
@PostMapping(value = "/test")
public ResponseEntity<Product> test(@RequestBody Product product) throws Exception {
productRepositry.save(product);
return new ResponseEntity(productRepositry.findAll(),OK);
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
public class Product {
@Id
@Column(name = "SKU_ID")
String skuId=null;
@ManyToOne
@JoinColumn(name = "category_id")
@JsonManagedReference
Category category;
@Column(name = "description")
String description=null;
@JsonManagedReference
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "stock_id", referencedColumnName = "id")
Stock stock=null;
@JsonManagedReference
@OneToMany(mappedBy = "product", fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
Set<ProductDetails> productDetails;
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
public class Stock {
@Id
@Column(name = "id")
Integer stockId;
@Column(name = "inventory_count")
Integer inventoryCount;
@Column(name = "self_count")
Integer selfCount;
@JsonBackReference
@OneToOne(mappedBy = "stock",cascade = CascadeType.ALL)
Product product;
}
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Category {
@Id
@Column(name = "category_id")
Integer categoryId;
@Column(name = "category_name")
String categoryName;
@OneToMany(mappedBy = "category", fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
@JsonBackReference
List<Product> product;
@Override
public String toString() {
return "Category{" +
"categoryId=" + categoryId +
", categoryName='" + categoryName + '\'' +
", product=" + product +
'}';
}
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
public class ProductDetails {
@Id
@Column(name = "id")
Integer productDetailId;
@Column(name = "cost")
Double cost;
@Column(name = "currency")
String currency;
@Column(name = "expiry_date")
Date expiryDate;
Integer supplierCode;
@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY)
Product product;
}
Solution
I think you have missed adding @JoinColumn
on Product
in ProductDetails
entity
@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SKU_ID")
Product product;
Also, you can try be removing @JsonManagedReference from Category
or Stock
Answered By - SSK