【问题标题】:EntityManager does not update on flush()EntityManager 不会在 flush() 上更新
【发布时间】:2011-02-26 21:15:15
【问题描述】:

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

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

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

代码:

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;
}

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

【问题讨论】:

    标签: jpa entity-relationship flush entitymanager


    【解决方案1】:

    您似乎在CustomerBuys 之间存在双向一对多关联,但我看不到您将Buys 实例添加到buysCollectionCustomer 一侧的位置.我希望看到这样的东西:

    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;
    }
    

    并确保您implement equals (and hashCode) properly

    我建议检查1.2.6. Working bi-directional links(并按照建议添加防御性链接管理方法)。

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 2012-03-11
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      相关资源
      最近更新 更多