Sajee
Sajee

Reputation: 4447

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?

Upvotes: 1

Views: 2792

Answers (2)

James
James

Reputation: 18379

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.

i.e.

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

Upvotes: 2

Adeel Ansari
Adeel Ansari

Reputation: 39897

Try <shared-cache-mode>NONE</shared-cache-mode>, in your persistence.xml.

Upvotes: 2

Related Questions