title: Obtaining EntityManager in Spring + Hibernate configuration
body: <p>I have a Spring MVC 4.0 application, and I am learning JPA. I use Hibernate as the JPA implementation.</p>
  
  <p>I can configure Hibernate as described in <a href="http://www.baeldung.com/hibernate-4-spring" rel="noreferrer">this</a> tutorial. It works fine, but I have to use Hibernate's <code>Session</code> object:</p>
  
  <pre><code>@Autowired
  SessionFactory sessionFactory;
  
  ...
  
  Session session = sessionFactory.openSession();
  </code></pre>
  
  <p>Now, I want to use JPA's <code>EntityManager</code> instead. I have followed <a href="http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/" rel="noreferrer">this</a> tutorial on the same web site (the configuration is very similar). And I tried to obtain an <code>EntityManager</code> object this way:</p>
  
  <pre><code>@PersistenceContext
  EntityManager entityManager;
  </code></pre>
  
  <p>I got a runtime message:</p>
  
  <pre><code>java.lang.IllegalStateException: No transactional EntityManager available
  </code></pre>
  
  <p>Then, I followed the suggestion in <a href="https://stackoverflow.com/a/17981359">this</a> answer, and tried to use the following code:</p>
  
  <pre><code>@PersistenceContext
  EntityManager entityManager;
  
  ...
  
  entityManager=entityManager.getEntityManagerFactory().createEntityManager();
  </code></pre>
  
  <p>It works a few times (about 9 repetitive method invocations), and then the application freezes.</p>
  
  <p>What is the right way to get <code>EntityManager</code> in Spring + Hibernate configuration?</p>
  
  <p>I do not need any Spring transaction functionality for now. I just want to get an access to <code>EntityManager</code> and play with JPA.</p>
  
  <p><strong>Spring/Hibernate configuration file (hibernate.xml)</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"&gt;
  
      &lt;bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/test_db" /&gt;
          &lt;property name="username" value="test" /&gt;
          &lt;property name="password" value="test" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan" value="net.myproject" /&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
          &lt;/property&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt;
  
      &lt;tx:annotation-driven /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>The class where I attempt to use <code>EntityManager</code></strong></p>
  
  <pre><code>@Repository
  public class ProductsService {
  
      @PersistenceContext
      EntityManager entityManager;
  
      @Transactional
      public GridResponse&lt;Product&gt; getProducts(GridRequest dRequest) {
  
          // The following line causes the exception: "java.lang.IllegalStateException: No transactional EntityManager available"
          Session session = entityManager.unwrap(Session.class);
  
          //...
      }
  
  ...
  </code></pre>