hibernate jpa 配置model,一对多的关系,保存主表时级联保存子表,子表中的主表外键为空。难道要先保存主表,然后手动去赋子表的外键?这还叫级联保存吗。求解!!!
例如:
[code="java"]
public class Order extends AbstractNsSimpleModel {
private static final long serialVersionUID = -3227571634593243399L;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "order", orphanRemoval = true)
private Set<OrderItem> orderItems = new HashSet<OrderItem>();
public Set<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(Set<OrderItem> orderItems) {
this.orderItems = orderItems;
}
}
[/code]
[code="java"]
public class OrderItem extends AbstractNsSimpleModel {
private static final long serialVersionUID = -7120458190166247286L;
@Column(name = "ORDER_ID", insertable = false, updatable = false, nullable = false)
private Long orderId;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "ORDER_ID", referencedColumnName = "ID")
private Order order;
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
[/code]
保存order时,级联保存orderItem,orderItem是被保存了,但是外键orderId为null。