使用容器管理的 EntityManager 进行会话管理

Session management with a container managed EntityManager

需要延迟获取集合。如果我随后尝试访问它的组件,我会得到以下异常:

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

所以我通过这种方式从 entityManager 中检索了会话:

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

但这会导致以下异常:

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

我在 Karaf 容器中使用 Hibernate。我还能如何继续浏览延迟获取的集合,或者使用容器管理的 EntityMananger 启动会话?

您似乎在 EM 上下文之外使用该实体。例如。如果它用于某些 jsp 等。 请在将集合发送出 EM 上下文之前调用集合的 getter 方法。