title: No transactional EntityManager available - working with JPA Api, error with Hibernate Session
body: <p>I am trying to unwrap the Hibernate Session from injected EntityManager, as I need to use Hibernate's native Criteria API.</p>
  
  <p>When I try to use Criteria i get following exception:</p>
  
  <pre><code>Caused by: java.lang.IllegalStateException: No transactional EntityManager available
  at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:224)
  at com.sun.proxy.$Proxy28.unwrap(Unknown Source)
  at sk.uniba.ais2.fajr.dao.impl.PouzivatelDAOHibernate.findByLogin(PouzivatelDAOHibernate.java:22)
  at sk.uniba.ais2.fajr.bo.PouzivatelService.findByLogin(PouzivatelService.java:20)
  at sk.uniba.ais2.fajr.mvc.controller.FooController.getFoooFOO(FooController.java:38)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
  at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
  at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
  ... 62 more
  </code></pre>
  
  <p>However, when I use the Query api from EntityManager itself, everything works just fine.</p>
  
  <p>My configuration:</p>
  
  <p><strong>db.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:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
      default-autowire="byName" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;!-- Scans within the base package of the application for @Components to 
          configure as beans --&gt;
      &lt;bean id="placeholderConfig"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="location" value="classpath:db.properties" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close"&gt;
      &lt;property name="driverClass" value="${db.driver}" /&gt;
      &lt;property name="jdbcUrl" value="${db.url}" /&gt;
      &lt;property name="user" value="${db.username}" /&gt;
      &lt;property name="password" value="${db.password}" /&gt;
  &lt;/bean&gt;
  
      &lt;bean id="jpaVendorAdapter"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
          &lt;property name="showSql" value="true" /&gt;
          &lt;property name="database" value="ORACLE" /&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="jpaVendorAdapter" ref="jpaVendorAdapter" /&gt;
          &lt;property name="packagesToScan" value="sk.uniba.ais2.fajr.entities" /&gt;
          &lt;property name="jpaPropertyMap"&gt;
              &lt;map&gt;
                  &lt;entry key="hibernate.default_schema" value="AIS2" /&gt;
              &lt;/map&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;tx:annotation-driven transaction-manager="transactionManager"
          proxy-target-class="true" /&gt;
      &lt;jpa:repositories base-package="sk.uniba.ais2.fajr.dao" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>PouzivatelDAOHibernate which does not work:</strong></p>
  
  <pre><code>    @Repository
  public class PouzivatelDAOHibernate implements PouzivatelDAO {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      @Override
      public Pouzivatel findByLogin(String login) {
          Criteria criteria = entityManager.unwrap(Session.class).createCriteria(Pouzivatel.class);
          return (Pouzivatel) criteria.add(Restrictions.eq("login", login)).uniqueResult();
      }
  
  }
  </code></pre>
  
  <p><strong>PouzivatelDAOHibernate which DOES work:</strong></p>
  
  <pre><code>@Repository
  public class PouzivatelDAOHibernate implements PouzivatelDAO {
  
      @PersistenceContext
      private EntityManager entityManager;
  
      @Override
      public Pouzivatel findByLogin(String login) {
  //      Criteria criteria = entityManager.unwrap(Session.class).createCriteria(Pouzivatel.class);
  //      return (Pouzivatel) criteria.add(Restrictions.eq("login", login)).uniqueResult();
          Query query = entityManager.createQuery("select p from Pouzivatel p where login=?");
          query.setParameter(1, login);
          return (Pouzivatel) query.getSingleResult();
      }
  
  }
  </code></pre>
  
  <p>EDIT: I am using Spring Data JPA</p>