如何从父序列值填充子复合键(Hibernate、JPA)?

发布于 2024-12-11 08:33:52 字数 1129 浏览 0 评论 0原文

我遇到的情况是,我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏雨凉 2024-12-18 08:33:52

最终在另一篇文章的帮助下解决了这个问题:
onetomany-and-composite-primary-keys
答案其实很简单。我们需要使用 Casade 注释将 Id 级联到子对象。因此,上面的一对多映射变为:

@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy = "orders",targetEntity = Items.class) public Set<Items> getItems() {     return this.items; } 

实施此检查时,请检查不同的级联类型。

祝你好运

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:

@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy = "orders",targetEntity = Items.class) public Set<Items> getItems() {     return this.items; } 

When implementing this review the different cascade types.

Good luck

美人如玉 2024-12-18 08:33:52

根据您的问题,我不是 100% 确定,但我认为您正在寻找 EntityManager.flush():

// Create an Order with no items, get its order ID
Order order = new Order(...);
em.persist(order);
// Order should have the generated ID filled in after this call to flush()
em.flush();  

// Create all the items for an order, filling in the Order ID into each item's
// composite key
for(...) {
    // create new item
    Item item = new Item(...);
    // populate other parts of the Item's composite key here
    // then fill in the OrderID part of the composite key
    item.getId().setOrderId(order.getOrderId());
    // add new item to the order's collection
    order.addItem(item);
}

填写项目后您不应该需要再次调用 persist() 。

I'm not 100% sure based on your question, but I think you're looking for EntityManager.flush():

// Create an Order with no items, get its order ID
Order order = new Order(...);
em.persist(order);
// Order should have the generated ID filled in after this call to flush()
em.flush();  

// Create all the items for an order, filling in the Order ID into each item's
// composite key
for(...) {
    // create new item
    Item item = new Item(...);
    // populate other parts of the Item's composite key here
    // then fill in the OrderID part of the composite key
    item.getId().setOrderId(order.getOrderId());
    // add new item to the order's collection
    order.addItem(item);
}

You shouldn't need another call to persist() after filling in the items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文