I’m using JPA for the data access layer of my application.
I have a DAO class for each table (entity), and in each of them I get the entity manager by
the @PersistenceContext annotation.
But recently I’ve read that this means that I have a cached version of the DB for each of my DAO classes, which is obviously not ideal from the performance point of view.
What is the best practice in this case (evidently, putting all the DAO methods in one huge class is not an option)? Any further reading on the topic?
The best practice is to inject a persistence context to classes that need them – let the container handle doing that properly.
There are various options for CDI: