Are entities cached in jpa by default?

TCM picture TCM · Apr 25, 2010 · Viewed 23k times · Source

I add entity to my database and it works fine. But when i retrieve the List, i get the old entity, the new entities i add are not shown until i undeploy the application and redeploy it again. This means are my entities cached by default? But, I haven't made any settings for caching entities in my persistence.xml or any such file.

I have even tried calling flush(), refresh() and merge(). But still it shows the old entities only. Am i missing something? Please help me.

Answer

cletus picture cletus · Apr 25, 2010

Welcome to JPA. If you use it, it means you will have huge problems if you update the database outside of JPA unless you know what you're doing and are very careful. This means you have to figure out how to flush any cached entities so they can be reloaded.

Basically, don't update entities outside JPA if you can at all help it and if you do you will probably have to get into the workings of the caching model used by your particular JPA provider. If you need to update outside JPA a lot then JPA probably isn't the right choice for you.