You are explicitly trying to manage foreign key in DetailOrder class , which is causing the issues. let spring data jpa take care of it automatically as you are using @MappedCollection in your Order class. So you need to remove below code from your DetailOrder class.
@Column(value = "order_id") private Long orderId;
And also remove it from your constructor as well , remove this line this.orderId = orderId;
Also, i see that there is no need for having keyColumn = "id" in your @MappedCollection in your Order class , so remove it .
@MappedCollection(idColumn = "order_id") private List detailOrders;
Your updated code should be like this for DetailOrder class and Order class :
@Table(name = "products") public class DetailOrder {
@Id
private Long id;
@Column(value = "product_article")
private Long productArticle;
@Column(value = "product_name")
private String productName;
@Column(value = "product_amount")
private int productAmount;
@Column(value = "product_price")
private int productPrice;
public DetailOrder(Long id, Long productArticle, String productName, int productAmount, int productPrice) {
this.id = id;
this.productArticle = productArticle;
this.productName = productName;
this.productAmount = productAmount;
this.productPrice = productPrice;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getProductArticle() {
return productArticle;
}
public void setProductArticle(Long productArticle) {
this.productArticle = productArticle;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getProductAmount() {
return productAmount;
}
public void setProductAmount(int productAmount) {
this.productAmount = productAmount;
}
public int getProductPrice() {
return productPrice;
}
public void setProductPrice(int productPrice) {
this.productPrice = productPrice;
}
}
@Table(name = "orders") public class Order {
@Id
@Column("id")
private Long id;
@Column(value = "order_number")
private String orderNumber;
@Column(value = "total_amount")
private Long totalAmount;
@Column(value = "order_date")
private Date orderDate;
@Column(value = "customer_name")
private String customerName;
@Column(value = "address")
private String deliveryAddress;
@Column(value = "payment_type")
private String paymentType;
@Column(value = "delivery_type")
private String deliveryType;
@MappedCollection(idColumn = "order_id")
private List<DetailOrder> detailOrders;
public Order(Long id, String orderNumber, Long totalAmount, Date orderDate, String customerName, String deliveryAddress, String paymentType, String deliveryType, List<DetailOrder> detailOrders) {
this.id = id;
this.orderNumber = orderNumber;
this.totalAmount = totalAmount;
this.orderDate = orderDate;
this.customerName = customerName;
this.deliveryAddress = deliveryAddress;
this.paymentType = paymentType;
this.deliveryType = deliveryType;
this.detailOrders = detailOrders;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public Long getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(Long totalAmount) {
this.totalAmount = totalAmount;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getDeliveryAddress() {
return deliveryAddress;
}
public void setDeliveryAddress(String deliveryAddress) {
this.deliveryAddress = deliveryAddress;
}
public String getPaymentType() {
return paymentType;
}
public void setPaymentType(String paymentType) {
this.paymentType = paymentType;
}
public String getDeliveryType() {
return deliveryType;
}
public void setDeliveryType(String deliveryType) {
this.deliveryType = deliveryType;
}
public List<DetailOrder> getDetailOrders() {
return detailOrders;
}
public void setDetailOrders(List<DetailOrder> detailOrders) {
this.detailOrders = detailOrders;
}
}