2

I'm using JPA for my Enterprise Application, and when I change values of row into my database table the related entity (get it by a find(-)) isn't updated with new value.

I think that there is a problem of sync between entity and database or a simple cache, so, how can I solve it?

Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
CeccoCQ
  • 3,746
  • 12
  • 50
  • 81
  • 1
    Is value changed in the DB? You should elaborate question - give example what are you doing. – viktor Dec 27 '11 at 11:48
  • Values was changed manually and directly into database. When I execute find from my JPA class, I get old data. – CeccoCQ Dec 27 '11 at 11:51

1 Answers1

2

If you edit the database row not using the JPA you should invoke EntityManager#refresh(-) using the entity fetched by find(-) as a parameter or invoke EntityManager#clear(-) and then invoke the find(-) again.

Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
  • I've tried and works correctly, but there are a method to avoid to call "refresh" for each query? – CeccoCQ Dec 27 '11 at 12:10
  • Well, there isn't any simple solution. You're updating your database outside of the JPA context, so the JPA doesn't know about the changes made - that's bad. Without caching, you'd hit the database with each request. Here you can find more information about [L1 cache](http://en.wikibooks.org/wiki/Java_Persistence/Caching#1st_Level_Cache). You might as well read your JPA provider documentation and see if it doesn't define any property for disabling L1 cache. – Piotr Nowicki Dec 27 '11 at 12:19