如何从父序列值填充子复合键(Hibernate、JPA)?
我遇到的情况是,我有一个名为 Order 的类,带有生成的 OrderID :
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_gen")
@SequenceGenerator(name = "seq_gen", sequenceName = "OID_SEQ1")
@Column(name = "ORDER_ID",nullable = false)
public long getOrderId()
{
return this.OrderId;
}
它还有一个与名为 items 的类/表的一对多映射:
@OneToMany(fetch = FetchType.LAZY,mappedBy = "orders",targetEntity = Items.class)
public Set<Items> getItems()
{
return this.items;
}
在 items 类中是一个嵌入式/复合 id:
@EmbeddedId
@AttributeOverrides({@AttributeOverride(name = "OrderId",column = @Column(name = "ORDER_ID",nullable = false)),
@AttributeOverride(name = "role",column = @Column(name = "ROLE",nullable = false,length = 10))})
public ItemId getId()
{
return this.id;
}
我的问题是该 id 是父 Order.class 的主键。换句话说,序列值。这意味着我无法在任何地方以及当我调用时设置此值:
entitymanager.persist(order);
父 Order 对象写入数据库,但 Set 中的任何项目都不会保留。只有一条插入语句,并且 Items 对象中对 orderId 的所有引用都为空???
如何使用生成的 Seq ID 填充这些 ID?
非常感谢您对此的任何帮助!
I have a situation were I have a class Called Order with a generated OrderID :
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_gen")
@SequenceGenerator(name = "seq_gen", sequenceName = "OID_SEQ1")
@Column(name = "ORDER_ID",nullable = false)
public long getOrderId()
{
return this.OrderId;
}
it also has a one-to-many mapping with a class/ table called items:
@OneToMany(fetch = FetchType.LAZY,mappedBy = "orders",targetEntity = Items.class)
public Set<Items> getItems()
{
return this.items;
}
within the items class is an embedded/composite id:
@EmbeddedId
@AttributeOverrides({@AttributeOverride(name = "OrderId",column = @Column(name = "ORDER_ID",nullable = false)),
@AttributeOverride(name = "role",column = @Column(name = "ROLE",nullable = false,length = 10))})
public ItemId getId()
{
return this.id;
}
My problem is part of this id is the primary key of the parent Order.class. In other words the sequence value. This means I cannot set this value anywhere and when I call:
entitymanager.persist(order);
The parent Order object writes to the db BUT none of the items within the Set are persisted. There is only one insert statement and all the references to orderId in the Items objects are null????
How can I populate these ID's with the generated Seq ID?
Many thanks for any help with this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终在另一篇文章的帮助下解决了这个问题:
onetomany-and-composite-primary-keys
答案其实很简单。我们需要使用 Casade 注释将 Id 级联到子对象。因此,上面的一对多映射变为:
实施此检查时,请检查不同的级联类型。
祝你好运
Eventually figured this out with the help of another post:
onetomany-and-composite-primary-keys
The answer was in fact quite simple. We need to use the Casade annotation to casade the Id down to the child object. so the one to many mapping above becomes:
When implementing this review the different cascade types.
Good luck
根据您的问题,我不是 100% 确定,但我认为您正在寻找 EntityManager.flush():
填写项目后您不应该需要再次调用 persist() 。
I'm not 100% sure based on your question, but I think you're looking for EntityManager.flush():
You shouldn't need another call to persist() after filling in the items.