I have a problem in Order Item order_id
its null because I create OrderItems
inside order like this the JSON for order
{
"supplier": {
"supplierId": 1,
"supplierName": "yahya",
"country": "Algeria",
"region": "undefined",
"productIds": [142, 141]
},
"orderItems": [
{
"productId": 1,
"quantity": 2,
"order_price": 10
},
{
"productId": 2,
"quantity": 1,
"order_price": 15
}
],
"orderType": "IMPORT",
"orderDate": "2024-05-10",
"reference": "ABC123",
"total_price": 35
}
package com.example.importexportservice.models;
import jakarta.persistence.*;
@Entity
@Table(name = "order_item")
public class OrderItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "order_item_id")
private Long id;
@ManyToOne
@JoinColumn(name = "order_id")
private Order order;
@Column(name = "product_id")
private Long productId;
@Column(name = "quantity")
private int quantity;
@Column(name = "Order_price")
private int Order_price;
public OrderItem() {
}
public OrderItem(Long id, Order order, Long productId, int quantity, int order_price) {
this.id = id;
this.order = order;
this.productId = productId;
this.quantity = quantity;
this.Order_price = order_price;
}
}
package com.example.importexportservice.models;
import jakarta.persistence.*;
import java.util.Date;
import java.util.List;
@Entity
@Table(name = "order_table")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "order_id")
private Long orderId;
@ManyToOne
@JoinColumn(name = "supplier_id")
private Supplier supplier;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL ,fetch = FetchType.EAGER)
private List<OrderItem> orderItems;
public List<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<OrderItem> orderItems) {
this.orderItems = orderItems;
}
@Enumerated(EnumType.STRING)
@Column(name = "order_Type")
private OrderType orderType;
public Order(Long orderId, Supplier supplier, List<OrderItem> orderItems, OrderType orderType, Date orderDate, String reference, int total_price) {
this.orderId = orderId;
this.supplier = supplier;
this.orderItems= orderItems;
this.orderType = orderType;
this.orderDate = orderDate;
this.reference = reference;
Total_price = total_price;
}
@Temporal(TemporalType.DATE)
@Column(name = "order_date")
private Date orderDate;
@Column(name = "order_reference")
private String reference;
@Column(name = "order_Total_price")
private int Total_price;
public Order() {
}
}
So I found order id in orderitem null even I tried to loop the list in the constructeur and affect the order id but still I found it null. Do I have the mapping wrong or can I do something else?
New contributor
Yahya Saoud is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.