【问题标题】:SessionFactory.getCurrentSession with EntityManager?SessionFactory.getCurrentSession 与 EntityManager?
【发布时间】:2015-01-22 09:39:58
【问题描述】:

我从使用 Hibernate API 切换到了 JPA API。现在我想知道,因为 EntityMangager(Factory) 没有任何类似 SessionFactory#getCurrentSession 的方法。

是否有任何与 EntityManger 类似的内置方法?

【问题讨论】:

  • 你能举一个例子,一旦你有了这个会话,你会怎么做?使用实体管理器大概可以完成所有事情,我从来不需要直接使用会话
  • 我使用 sesson per request 模式,在 ServletFilter 中管理会话并使用 SessionFactory#getCurrentSession 访问它们。

标签: hibernate session jakarta-ee jpa


【解决方案1】:

您可以使用Session session = entityManager.unwrap(Session.class) 获取会话,其中Sessionorg.hibernate.Session

【讨论】:

  • 我不需要显式会话我只需要在 http 请求范围(例如 Threadlocal 范围)中使用的“当前持久性对象”(=EntityManager)。如前所述,这是通过实现 Session per Request 模式并在移动到 EntityManager 之前使用 SessionFactory#getCurrentSession 来实现的
  • 您可以使用ThreadLocals 来做到这一点,查看this post 了解更多信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多