Issue
Entity Manager is a wrapper that is used by hibernate session for persisting objects. Session has a Persistent Context, in which it saves objects ( like in cache ). Session is closed and created every time the transaction ends/starts. Cache is cleared after transaction. This is called 1 level cache by Hibernate.
I would appreciate that a lot if you could tell me if I understand that right.
Solution
I hope you will find this book helpful, it's worth it.
Reference: "Harnessing Hibernate" by James Elliot, Tim O'Brien, Ryan Fowler, P. 49
Find it on Amazon: https://www.amazon.com/Harnessing-Hibernate-Step-step-Persistence/dp/0596517726/
... "When it comes to actually perform persistence, we ask the SessionFactory to open a Session for us, which establishes a JDBC connection to the database and provides us with a context in which we can create, obtain, manipulate, and delete persistent objects. As long as the session is open, a connection to the database is maintained, and changes to the persistent objects associated with the session are tracked so that they can be applied to the database when the session is closed. Conceptually, you can think of a session as a 'large-scale transaction' between the persistent objects and the database, which may encompass several database level transactions. As with a database transaction, though, you should not think about keeping Hibernate session open over long periods of application existence (such as a while you're waiting for user input). A single session is used for a specific and bounded operation in the application, something like populating the user interface or making a change that has committed by the user." ...
Also, you might find this documentation helpful as well.
Entity Manager: https://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/
Answered By - Erion Omeri