hibernate @ManyToOne 无法正确插入,因为 FK id 尚未创建
在 Order 类中我有一个属性:
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "order")
private Set<OrderLine>
orderLines;
在 OrderLine 类中我有一个属性:
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name="OrderID")
private Order order;
在数据库中,orderline 只是有一个指向订单的 FK。这就是问题所在,
我手动构建了一个 Order 对象,没有 ID,因为它设置为自动生成,我使用适当的 DAO 保存它。订单表已正确填写,但订单行表未正确填写。
订单行表中订单的 FK 字段留空。我认为这是因为在插入时订单 ID 仍然没有 ID。
有没有简单的方法来解决这个问题?
In the class Order I have a property:
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "order")
private Set<OrderLine>
orderLines;
In the class OrderLine I have a property:
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name="OrderID") private Order order;
In the DB, orderline simply has a FK pointing to an order. And this is where the issue lies
I manually build an Order object, without an ID because it's set to auto generate, which I save using the appropriate DAO. The order table is filled correctly but the orderline table isn't.
The FK field of the order in orderline table is left blank. I assume it's because at the point of insert the order ID still has no ID.
Is there a simple way to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何保存订单行实例?
您必须在订单行实例中设置订单对象的实例。
例如:
How do you persist your orderline instances?
You will have to set the instance of the order object in the orderline instance.
For example: