它是我的实体字段。结构类似->
购物车[cartid=1,userid=1,ordeitem=[orderitem[pid=12,qty=3,cart=null],orderitem[pid=4,qty=5,cart=null]]
@Entity
@Table(name = "cart")
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long cartId;
@Column(nullable = false, unique = false)
private Long userId;
@ElementCollection
// @OneToMany(targetEntity=OrderItem.class, mappedBy="pid", fetch=FetchType.EAGER)
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pid")
private List<OrderItem> ordeItem;
public Cart(){}
public Cart(Long cartId, Long userId, List<OrderItem> ordeItem) {
super();
this.cartId = cartId;
this.userId = userId;
this.ordeItem = ordeItem;
}
@Override
public String toString() {
return "Cart [cartId=" + cartId + ", userId=" + userId + ", ordeItem=" + ordeItem + "]";
}
public Long getCartId() {
return cartId;
}
public void setCartId(Long cartId) {
this.cartId = cartId;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public List<OrderItem> getOrdeItem() {
return ordeItem;
}
public void setOrdeItem(List<OrderItem> ordeItem) {
this.ordeItem = ordeItem;
}
@Entity
static class OrderItem {
@Id
private Long pid;
private Long qty;
@ManyToOne(fetch = FetchType.EAGER, targetEntity = Cart.class)
@JoinColumn(name = "cart_id", nullable = false)
private Cart cart;
public OrderItem(Long pid, Long qty, Cart cart) {
super();
this.pid = pid;
this.qty = qty;
this.cart = cart;
}
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public Long getQty() {
return qty;
}
public void setQty(Long qty) {
this.qty = qty;
}
public Cart getCart() {
return cart;
}
public void setCart(Cart cart) {
this.cart = cart;
}
@Override
public String toString() {
return "OrderItem [pid=" + pid + ", qty=" + qty + ", cart=" + cart + "]";
}
}
}
通过 Postman 传递下面的数据
{
"userId": 1,
"ordeItem": [
{
"pid": 12,
"qty": 3,
},
{
"pid": 4,
"qty": 5,
}
]
}
正在尝试将其保存到mysql数据库中。但在使用中保存时出现上述错误。
在服务中,我正在尝试:
public Cart addCart(Cart cart) {
System.out.println(cart);
cartDao.save(cart);
return cart;
}
但不幸的是,cartid得到了null值,这就是为什么会出现问题的原因。但找不到实际问题。
暂无答案!
目前还没有任何答案,快来回答吧!