Search code examples
javahibernatejpaapache-karafkaraf

Session management with a container managed EntityManager


A collection needs to be fetched lazily. If I then try to access its components, I get the following Exception :

failed to lazily initialize a collection of role:
 mapp3.model.ProductDefinition, could not initialize proxy - no Session

So I retrieved the session from the entityManager this way:

Session session = entityManager.unwrap(Session.class);

but this causes the following exception :

Error executing command: Transaction management is not available for container managed EntityManagers.

I am using Hibernate inside a Karaf container. How else could I proceed to either be able to browse the lazily fetched collection, either to start a Session with a container managed EntityMananger ?


Solution

  • Looks like you're using the entity outside of EM context. eg. if it used in some jsp etc. Please invoke getter method for the collection right before you're sending it out of EM context.