1

Java EJB 的 EntityManager 不会更新来自消费者的数据。

消费者登录商店,购买了一些东西并想查看他的购物历史。除了他最后一次购买外,一切都显示出来了。如果他注销并登录,它会显示。

我使用 JPA 将购买/购买(映射到消费者)持久化到数据库。似乎无法检测到来自此会话的购买。

代码:

public Buys buyItem(Consumer c, int amount) {
    Buys b = new Buys();
    b.setConsumerId(c);
    b.setContent("DVD");
    b.setPrice(amount);
    em.persist(b);
    em.flush();
    return b;
}

public Collection getAllBuysFromUser(Consumer consumer) {
   Collection<Buys> collection = consumer.getBuysCollection();
   return collection;
}

帮助!?同花顺是不行的……

4

1 回答 1

2

您似乎在 and 之间具有双向一对多关联CustomerBuys但我看不到您将Buys实例添加到buysCollection侧面的Customer位置。我希望看到这样的东西:

public Buys buyItem(Consumer c, int amount) {
    Buys b = new Buys();
    b.setConsumerId(c);
    b.setContent("DVD");
    b.setPrice(amount);
    c.getBuysCollection().add(b);
    em.persist(b);
    em.flush();
    return b;
}

并确保您正确实施equals(和hashCode

我建议检查1.2.6。工作双向链接(并按照建议添加防御性链接管理方法)。

于 2010-06-02T16:15:20.967 回答