英文:
Insert a record into multiple Table using JPA
问题
我有三个实体,分别是
- 产品(product)
- 产品详细信息(product details)
- 库存(stock)
- 类别(category)
当我尝试获取产品的详细信息时,一切正常,但当我尝试保存时,在控制台中显示以下错误:
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)
我的问题是如何将产品添加到数据库中以及如何优化我的实体。
输入
{
"skuId": "2",
"category": {
"categoryId": 2,
"categoryName": "食品"
},
"description": "牛奶",
"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
}
]
}
控制器方法
@PostMapping(value = "/test")
public ResponseEntity<Product> test(@RequestBody Product product) throws Exception {
productRepositry.save(product);
return new ResponseEntity(productRepositry.findAll(), OK);
}
产品类(Product)
@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;
}
库存类(Stock)
@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;
}
类别类(Category)
@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 +
'}';
}
}
产品详细信息类(ProductDetails)
@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;
}
请让我知道如果你还需要其他信息。
英文:
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;
}
答案1
得分: 0
我认为您忘记在“ProductDetails”实体中的“Product”上添加`@JoinColumn`。
@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SKU_ID")
Product product;
另外,您可以尝试从“Category”或“Stock”中移除@JsonManagedReference。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论