情况看起来像这样。我在MySQL中有2个表(密码和顺序),在Spring中有对应于它们的实体。他们在这里:
食谱:
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Set;
@Entity
@Table(name = "recip")
public class Recip {
@Id
@GeneratedValue
private Long recip_id;
@NotNull
private double quantity;
@NotNull
private double price;
private double quantity_item;
private String name;
@OneToMany(mappedBy = "recip", cascade = CascadeType.ALL)
private Set<Orders> orders;
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnore
@JoinColumn(name = "storeitem_storeitem_id", nullable = false)
private StoreItem storeItem;
public Long getRecip_id() {
return recip_id;
}
public void setRecip_id(Long recip_id) {
this.recip_id = recip_id;
}
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getQuantity_item() {
return quantity_item;
}
public void setQuantity_item(double quantity_item) {
this.quantity_item = quantity_item;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Orders> getOrders() {
return orders;
}
public void setOrders(Set<Orders> orders) {
this.orders = orders;
}
public StoreItem getStoreItem() {
return storeItem;
}
public void setStoreItem(StoreItem storeItem) {
this.storeItem = storeItem;
}
}
和订单:
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.sql.Timestamp;
@Entity
public class Orders {
@Id
@GeneratedValue
private Long orders_id;
@Basic
private Timestamp date;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "recip_recip_id", nullable = false)
private Recip recip;
public Long getOrders_id() {
return orders_id;
}
public void setOrders_id(Long orders_id) {
this.orders_id = orders_id;
}
public Timestamp getDate() {
return date;
}
public void setDate(Timestamp date) {
this.date = date;
}
public Recip getRecip() {
return recip;
}
public void setRecip(Recip recip) {
this.recip = recip;
}
}
我需要获取的是双向OneToMany映射。目前,它以一种方式起作用。列出“ recip”中的数据时会得到什么:
[
{
"recip_id": 1,
"quantity": 500,
"price": 8.9,
"quantity_item": 0.5,
"name": "Carrot Juice",
"orders": [
{
"orders_id": 1,
"date": "2019-05-05T11:49:26.197+0000"
}
]
}
]
但是从订单中我只能得到:
[
{
"orders_id": 1,
"date": "2019-05-05T11:49:26.197+0000"
}
]
所以它不能以相反的方式工作。
对我来说,另一个问题是Angular(解决第一个问题之后)。我想在创建新订单时进行逻辑处理:列出所有食谱->从名称中选择食谱(例如胡萝卜汁)->将订单与食谱ID连接->将订单保存到数据库