我在Spring Boot
建立訂單跟蹤系統,採用Hibernate annotations
和Repositories
。我有一個Order
類,它可以有一個列表OrderItems
。這些分別映射到ORDER
和ORDER_ITEMS
表。我代表兩者的代碼如下。無法保存在Springboot一對多的關係的子對象/休眠
Order.java
package net.township.order;
import org.hibernate.annotations.Cascade;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
import java.util.Set;
@Entity
@Table(name = "orders")
public class Order {
public Order() {
}
public Order(long merchantId, String firstDeliveryName, String
lastDeliveryName, String deliveryAddress, String status, Date createDate,
Date updateDate) {
this.merchantId = merchantId;
this.lastDeliveryName = lastDeliveryName;
this.firstDeliveryName = firstDeliveryName;
this.deliveryAddress = deliveryAddress;
this.status = status;
this.createDate = createDate;
this.updateDate = updateDate;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "order_id", unique = true)
private long orderId;
@Column(name = "merchant_id")
private long merchantId;
@Column(name = "first_delivery_name")
private String firstDeliveryName;
@Column(name = "last_delivery_name")
private String lastDeliveryName;
@Column(name = "delivery_address")
private String deliveryAddress;
@Column
private String status;
@OneToMany(mappedBy = "order", cascade = {
CascadeType.ALL,CascadeType.PERSIST,CascadeType.MERGE })
private List<OrderItem> orderItems;
@Column(name = "create_date")
private Date createDate;
@Column(name = "update_date")
private Date updateDate;
public void setOrderId(long orderId) {
this.orderId = orderId;
}
public long getMerchantId() {
return merchantId;
}
public void setMerchantId(long merchantId) {
this.merchantId = merchantId;
}
public List<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<OrderItem> orderItems) {
this.orderItems = orderItems;
}
public String getLastDeliveryName() {
return lastDeliveryName;
}
public void setLastDeliveryName(String lastDeliveryName) {
this.lastDeliveryName = lastDeliveryName;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public String getFirstDeliveryName() {
return firstDeliveryName;
}
public void setFirstDeliveryName(String firstDeliveryName) {
this.firstDeliveryName = firstDeliveryName;
}
public String getDeliveryAddress() {
return deliveryAddress;
}
public void setDeliveryAddress(String deliveryAddress) {
this.deliveryAddress = deliveryAddress;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
OrderItem.java
package net.township.order;
import com.fasterxml.jackson.annotation.JsonBackReference;
import org.hibernate.annotations.Cascade;
import javax.persistence.*;
@Entity
@Table(name = "order_items")
public class OrderItem {
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
@Column
private String name;
@Column
private String description;
@Column
private Long quantity;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn (name="ORDER_ID")
@JsonBackReference
@Cascade(value={org.hibernate.annotations.CascadeType.ALL})
private Order order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getQuantity() {
return quantity;
}
public void setQuantity(Long quantity) {
this.quantity = quantity;
}
}
當我POST
從我的前端新Order
,它被正確地映射到Order
對象。在JSON
中提供的所有OrderItems
也作爲List
存在於對象中。但是,在使用我的OrderRepository
的保存方法(它僅僅是CrudRepository
)將其保存到數據庫後,我的數據庫包含一個帶有正確字段的新對象Order
,但在ORDER_ITEMS
中沒有創建任何對象。
我在Hibernate
和JPA OneToMany
批註的文檔中找到了相關的文檔,我沒有看到我在哪裏出錯。我還會補充說我沒有手動創建模式,讓SpringBoot
爲我設置H2
中的所有內容。
小評論:'CascadeType.ALL','CascadeType.PERSIST','CascadeType.MERGE'是有點多餘:'CascadeType的。 ALL'已包含其他類型。 – aUserHimself
你不能在兩個方向上級聯,你必須選擇一個方向作爲主方,一個方向作爲從機(如果你不這樣做,通常Hibernate會拋出一個錯誤)。默認情況下,集合始終是從屬。這是爲了避免Hibernate無法解決的不一致的模式。 –
您是否在保存之前爲OrderItems設置了屬性'order'? – Nikolay