We are developing a Java EE application using Hibernate as a JPA provider. We now wish to use Hibernate Criterias but for that I need to get access to the HibernateEntityManagerImpl.
We have this currently in our componentContext.xml
  <context:component-scan base-package="com.volvo.it.lsm"/>
<!-- JPA EntityManagerFactory -->
<bean id="EmployeeDomainEntityManagerFactory" parent="hibernateEntityManagerFactory"      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="EmployeeDomainPU" /> 
</bean>
and in our classes:
@PersistenceContext(unitName = "EmployeeDomainPU")
public void setEntityManager(EntityManager em) {
    this.em =开发者_StackOverflow社区 em;
}
Thanks!
EntityManager em ...
//if you want Session with JPA 1.0
org.hibernate.Session session = (org.hibernate.Session) em.getDelegate();
//If you use JPA 2.0 this is preferred way
org.hibernate.Session session2 = em.unwrap(org.hibernate.Session.class);
//if you really want Hibernates implementation of EntityManager, just cast
//it (this is not needed for Criteria queries though). Actual implementing class
//is of course Hibernates business and it can vary
org.hibernate.ejb.EntityManagerImpl emi = 
 (org.hibernate.ejb.EntityManagerImpl) em;
Use this code:
Session session = (Session) entityManager.getDelegate();
session.createCriteria(...);
Note that JPA2 has a (different) Criteria API as well. It's harder to use IMHO, but it's more type-safe, and has the same advantages as the Hibernate Criteria API (be able to dynamically compose a query)
You can call em.getDelegate() and cast it to the Hibernate class
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论