2010-06-02 63 views
1

Java EJB的EntityManager不会更新消费者的数据。EntityManager不会在flush()上更新()

消费者登录商店,购买一些东西,并希望看看他的购物历史。一切都显示,但他最后一次购买。如果他注销并进入,它会显示。

我已经使用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; 
} 

帮助!同花顺并不做的伎俩......

回答

2

你似乎有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(并按建议添加防御性链接管理方法)。