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?

2
  • 1
    Is value changed in the DB? You should elaborate question - give example what are you doing. Commented Dec 27, 2011 at 11:48
  • Values was changed manually and directly into database. When I execute find from my JPA class, I get old data. Commented Dec 27, 2011 at 11:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried and works correctly, but there are a method to avoid to call "refresh" for each query?
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. You might as well read your JPA provider documentation and see if it doesn't define any property for disabling L1 cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.