本文介绍了我如何获得会话对象,如果我有entitymanager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

  private EntityManager em; 

public List getAll(DetachedCriteria detachedCriteria){

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





$ b

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

解决方案

为了完全穷尽,如果您使用JPA 1.0或JPA 2.0执行。

JPA 1.0



使用JPA 1.0,您必须使用。但请记住, 此方法的结果是特定于实现的 ,即从使用Hibernate的应用程序服务器不可移植到另一个。例如使用JBoss 你会这样做:

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

但方法,该方法优先于用于新应用程序。



因此,使用Hibernate作为JPA 2.0实现(参见),您应该这样做:

 会话会话= entityManager.unwrap(Session.class ); 


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 ?

解决方案

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

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 specific i.e. non portable from application server using Hibernate to the other. For example with JBoss you would do:

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

But with GlassFish, you'd have to do:

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

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.

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

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

这篇关于我如何获得会话对象,如果我有entitymanager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:01