0

I am using Hibernate JPA 2.1 with swing application. My DB is MySQL. The same database could be update from another apring jpa application, which has no connection with my application. My problem is that my swing application always gives me old entity. How to configure it to be aware of the DB external changes.

It would be very helpful me if someone resolve this issue.

Thanks to all for watching and answering.

Cool Java guy מוחמד
  • 1,687
  • 4
  • 24
  • 40
  • You need to disable the caching in your application. If you're caching anything manually, you need to stop doing that too :). Hibernate usually uses EhCache, so a safe check is if you have any ehcache configuration and remove it. This question might give you another alternative (to use a stateless session): http://stackoverflow.com/questions/3827704/how-to-disable-hibernate-caching – Augusto May 22 '15 at 07:45
  • Or you're simply using one Session and keeping it open forever. Don't do that. Open a new session, and start a new transaction, every time you need to access the database. – JB Nizet May 22 '15 at 07:50

2 Answers2

0

You need to clear session cache:

@PersistenceContext
private EntityManager em;
void beforeQuery() {
    Session session = em.unwrap(Session.class);
    session.clear();
}
Vovka
  • 599
  • 3
  • 10
0

refresh method should do the job:

@PersistenceContext
private EntityManager em;
void refresh(MyEntity myEntity) {
    em.refresh(myEntity);
}

More information about refresh: https://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#refresh(java.lang.Object)

Emilien Brigand
  • 9,943
  • 8
  • 32
  • 37