【发布时间】:2020-12-28 20:06:57
【问题描述】:
我有两个表:SubmitOrder 和 OrderItem 表 SubmitOrder 包含 @OneToMany(OrderItem) 和 @Join Column(submitOrder id)。 表 OrderItem 有一个字段包含 SubmitOrder id。 结果只向 submit_order 表插入数据,而没有向 order_item 表插入数据。
不确定我搞砸了哪一部分。
提交订单实体:
@Entity
@Table(name="submit_order")
public class SubmitOrder {
// Define Fields
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="email")
private String email;
@Column(name="phone")
private String phone;
@Column(name="pickup")
private String pickup;
@Column(name="subtotal")
private double subtotal;
@Column(name="tax")
private double tax;
@Column(name="total")
private double total;
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name="order_id")
private List<OrderItem> orderItem;
public SubmitOrder() {}
public SubmitOrder(String name, String email, String phone, String pickup, double subtotal, double tax, double total) {
this.name = name;
this.email = email;
this.phone = phone;
this.pickup = pickup;
this.subtotal = subtotal;
this.tax = tax;
this.total = total;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPickup() {
return pickup;
}
public void setPickup(String pickup) {
this.pickup = pickup;
}
public double getSubtotal() {
return subtotal;
}
public void setSubtotal(double subtotal) {
this.subtotal = subtotal;
}
public double getTax() {
return tax;
}
public void setTax(double tax) {
this.tax = tax;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public List<OrderItem> getOrderItem() {
return orderItem;
}
public void setOrderItem(List<OrderItem> orderItem) {
this.orderItem = orderItem;
}
}
OrderItem 实体:
@Entity
@Table(name="order_item")
public class OrderItem {
// Define Fields
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="section")
private String section;
@Column(name="size")
private String size;
@Column(name="quantity")
private int quantity;
@Column(name="price")
private double price;
public OrderItem() {}
public OrderItem(String name, String section, String size, int quantity, double price) {
this.name = name;
this.section = section;
this.size = size;
this.quantity = quantity;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
从 angular 传过来的数据:
SubmitOrder {name: "Sam", email: "sam@email.com", phone: "221-442-3542", subTotal: 73.32, tax: 5.8656, …}
cartOrder: Array(8)
0: {id: 24, section: "Lo Mein", name: "Beef Lo Mein", size: "Small", price: 6.6, …}
1: {id: 29, section: "Dinner", name: "Peper steak with Onion", size: "Dinner", price: 8.46, …}
2: {id: 30, section: "Appetizers", name: "Vegetable Spring Roll (2 pc.)", size: "None", price: 2.42, …}
3: {id: 35, section: "Appetizers", name: "Vegetable Egg Roll (1 pc.)", size: "None", price: 1.32, …}
4: {id: 36, section: "Appetizers", name: "Roast Port Egg Roll (1 pc.)", size: "None", price: 1.32, …}
5: {id: 38, section: "Soup", name: "Wonton Soup", size: "Small", price: 2.42, …}
6: {id: 39, section: "Soup", name: "Egg Drop Soup", size: "Large", price: 3.2, …}
7: {id: 40, section: "Appetizers", name: "Chinese Donuts (10 pc.)", size: "None", price: 4.24, …}
length: 8
__proto__: Array(0)
email: "sam@email.com"
name: "Sam"
phone: "221-442-3542"
pickup: "ASAP"
subTotal: 73.32
tax: 5.8656
total: 79.1856
__proto__: Object
REST 控制器
@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:4200")
public class SubmitOrderRESTController {
private SubmitOrderService submitOrderService;
@Autowired
public SubmitOrderRESTController(SubmitOrderService theSubmitOrderService) {
submitOrderService = theSubmitOrderService;
}
@PostMapping("/submit")
public SubmitOrder saveOrder(@RequestBody SubmitOrder theSubmitOrder) {
theSubmitOrder.setId(0);
submitOrderService.save(theSubmitOrder);
return theSubmitOrder;
}
}
存储库
@Repository
public class SubmitOrderDAOImplement implements SubmitOrderDAO {
// Define field for entitymanager
private EntityManager entityManager;
public SubmitOrderDAOImplement(EntityManager theEntityManager){
entityManager = theEntityManager;
}
@Override
public void save(SubmitOrder theSubmitOrder) {
// Get the current hibernate session
Session currentSession = entityManager.unwrap(Session.class);
// Save rates
currentSession.saveOrUpdate(theSubmitOrder);
}
}
【问题讨论】:
-
您是否添加了有关该问题的更多详细信息?您是否有控制器和存储库但没有插入 OrderItem?
标签: java mysql angular spring hibernate