简体   繁体   中英

JPA: query not fetching the latest data

I'm finding that JPA is not fetching the latest data from the database.

My data model has a customer entity that owns a number of order entities. I'm persisting an order:

em.persist(order);
em.getTransaction().commit();
em.close();

To view the orders, I call:

Collection<Order> orders = customer.getOrderCollection();

The orders collection is missing the latest order. I've checked and the order is persisted to the database. With the driver logging turned on, I don't see any calls to the database when getOrderCollection() is called.

In Netbeans 6.9, when I stop & redeploy the app, I see the latest order that I persisted appear. So perhaps there's some sort of caching that may be interfering with getOrderCollection()? For some reason, JPA is not going to the database. Why?

How can I force JPA to go to the database when getOrderCollection() is called?

在persistence.xml中尝试<shared-cache-mode>NONE</shared-cache-mode>

You must maintain bidirectional relationships in your object model. When you add a new Order for a Customer, you must add the order to the Customer's orders.

ie

public void addOrder(Order order) {
  this.orders.add(order);
  order.setCustomer(this);
}

You could also refresh the object, or disable the cache, but fixing your code would be best.

See,

http://en.wikibooks.org/wiki/Java_Persistence/Caching

http://wiki.eclipse.org/EclipseLink/Examples/JPA/Caching

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM