0
我在hibernate jpa中的关系存在一些问题。我想使OneToMany关系,但是当我保存对象“一”在基地,然后在表“多”列包含外键总是空。 在我的情况,我有类客户:jpa中的一对多关系,外键永远为空
@OneToOne(fetch=FetchType.LAZY, mappedBy="customer", cascade = CascadeType.ALL)
private Address billingAddress;
@Column(name="phone_address")
private String phoneNumber;
@OneToMany(fetch=FetchType.EAGER, mappedBy="customer")
private List<Order> orders;
它包含订单(其中工程完美)一对多的关系。 订购类:
@Id
@GeneratedValue
@Column(name="order_id")
private Long orderId;
@OneToOne(fetch=FetchType.LAZY, mappedBy="order", cascade = CascadeType.ALL)
private Cart cart;
@ManyToOne
@JoinColumn(name="customer_id")
private Customer customer;
@OneToOne(fetch=FetchType.LAZY, mappedBy="order", cascade = CascadeType.MERGE)
private Address shippingDetail;
订购类包含车(OneToOne):
@Id
@GeneratedValue
@Column(name="base_id")
private Long baseId;
@Column(name="cart_id")
private String cartId;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "cart", cascade = CascadeType.ALL)
@MapKeyColumn(name = "items_keys")
private Map<Long, CartItem> cartItems;
@Column(name="grand_total")
private BigDecimal grandTotal;
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
private Order order;
这里是addCartItem方法车类:
public void addCartItem(CartItem item){
Long productId = item.getProduct().getProductId();
if(cartItems.containsKey(productId)){
CartItem existingCartItem = cartItems.get(productId);
existingCartItem.setQuantity(existingCartItem.getQuantity() + item.getQuantity());
cartItems.put(productId, existingCartItem);
}else{
cartItems.put(productId, item);
//item.setCart(this);
}
updateGrandTotal();
}
而我们在这里,购物车必须包含CartItem映射在OneToMany关系,这是行不通的。 CartItem类:
@Id
@GeneratedValue
private long id;
@OneToOne(fetch=FetchType.LAZY, mappedBy="cartItem", cascade = CascadeType.MERGE)
private Product product;
private int quantity;
private BigDecimal totalPrice;
@ManyToOne
private Cart cart;
据我知道我需要启动外键CartItem,我试图做到这一点在addCartItem方法,但是当我这样做,还有的StackOverflowError。我错过了什么或如何发起CartItem与购物车?
你可以看看帖子https://stackoverflow.com/questions/37114984/jpa-foreign-key-is-null-in-the-child-table – Akash
我知道我需要启动CartItem,但是这行代码已经被注释,导致StackOverFlowError正如我上面所写的。 –
我倾向于猜测其他地方有问题。特别是,'StackOverflowError'通常表示失控递归。你所提供的内容没有提供可能发生这种事情的线索。像往常一样,展示问题的[mcve]将是有用和适当的。 –