Java 如果我有实体管理器,我如何获取会话对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4148231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:04:56  来源:igfitidea点击:

How can i get the session object if i have the entitymanager

javahibernatejpahibernate-entitymanager

提问by storm_buster

I have

我有

private EntityManager em;

public List getAll(DetachedCriteria detachedCriteria)   {

    return detachedCriteria.getExecutableCriteria( ??? ).list();
}

How can i retrieve the session if am using entitymanager or how can i get the result from my detachedcriteria ?

如果使用 entitymanager 或如何从我的 detachedcriteria 中获取结果,我如何检索会话?

采纳答案by Pascal Thivent

To be totally exhaustive, things are different if you're using a JPA 1.0 or a JPA 2.0 implementation.

全面来说,如果您使用的是 JPA 1.0 或 JPA 2.0 实现,情况会有所不同。

JPA 1.0

JPA 1.0

With JPA 1.0, you'd have to use EntityManager#getDelegate(). But keep in mind that the result of this method is implementation specifici.e. non portable from application server using Hibernate to the other. For example with JBossyou would do:

使用 JPA 1.0,您必须使用EntityManager#getDelegate(). 但请记住, 此方法的结果是特定实现的,即不可从使用 Hibernate 的应用程序服务器移植到另一个。例如,使用 JBoss,您将执行以下操作:

org.hibernate.Session session = (Session) manager.getDelegate();

But with GlassFish, you'd have to do:

但是使用 GlassFish,您必须执行以下操作:

org.hibernate.Session session = ((org.hibernate.ejb.EntityManagerImpl) em.getDelegate()).getSession(); 

I agree, that's horrible, and the spec is to blame here (not clear enough).

我同意,这太可怕了,这里应该归咎于规范(不够清楚)。

JPA 2.0

JPA 2.0

With JPA 2.0, there is a new (and much better) EntityManager#unwrap(Class<T>)method that is to be preferred over EntityManager#getDelegate()for new applications.

在 JPA 2.0 中,有一种新的(而且更好)的EntityManager#unwrap(Class<T>)方法是EntityManager#getDelegate()新应用程序的首选方法。

So with Hibernate as JPA 2.0 implementation (see 3.15. Native Hibernate API), you would do:

因此,使用 Hibernate 作为 JPA 2.0 实现(请参阅3.15. Native Hibernate API),您将执行以下操作:

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

回答by Vladimir Ivanov

See the section "5.1. Accessing Hibernate APIs from JPA" in the Hibernate ORM User Guide:

请参阅Hibernate ORM 用户指南中的“ 5.1. 从 JPA 访问 Hibernate API”部分

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

回答by Enio Dantas

This will explain better.

这将更好地解释。

EntityManager em = new JPAUtil().getEntityManager();
Session session = em.unwrap(Session.class);
Criteria c = session.createCriteria(Name.class);

回答by carlos veintemilla

I was working in Wildfly but I was using

我在 Wildfly 工作,但我正在使用

org.hibernate.Session session = ((org.hibernate.ejb.EntityManagerImpl) em.getDelegate()).getSession();

and the correct was

正确的是

org.hibernate.Session session = (Session) manager.getDelegate();

回答by Hari Krishna

'entityManager.unwrap(Session.class)' is used to get session from EntityManager.

'entityManager.unwrap(Session.class)' 用于从 EntityManager 获取会话。

@Repository
@Transactional
public class EmployeeRepository {

  @PersistenceContext
  private EntityManager entityManager;

  public Session getSession() {
    Session session = entityManager.unwrap(Session.class);
    return session;
  }

  ......
  ......

}

Demo Application link.

演示应用程序链接