9

I am trying to unwrap the Hibernate Session from injected EntityManager, as I need to use Hibernate's native Criteria API.

When I try to use Criteria i get following exception:

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

However, when I use the Query api from EntityManager itself, everything works just fine.

My configuration:

db.xml

    <?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- Scans within the base package of the application for @Components to 
        configure as beans -->
    <bean id="placeholderConfig"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties" />
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="${db.driver}" />
    <property name="jdbcUrl" value="${db.url}" />
    <property name="user" value="${db.username}" />
    <property name="password" value="${db.password}" />
</bean>

    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true" />
        <property name="database" value="ORACLE" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="packagesToScan" value="sk.uniba.ais2.fajr.entities" />
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.default_schema" value="AIS2" />
            </map>
        </property>

    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />
    <jpa:repositories base-package="sk.uniba.ais2.fajr.dao" />
</beans>

PouzivatelDAOHibernate which does not work:

    @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();
    }

}

PouzivatelDAOHibernate which DOES work:

@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();
    }

}

EDIT: I am using Spring Data JPA

3 Answers 3

24

I've actually solved the problem (the solution was pretty trivial): use org.springframework.transaction.annotation.Transactional instead of javax.transaction.Transactional

9

Consider using @Transactional over your DAO methods. It seems that your configuration is correct.

3
  • I've also tried that, still nothing EDIT: But your answer was a part of the solution, upvote :) Mar 13, 2014 at 15:56
  • Nope :) stackoverflow.com/questions/1079114/… Mar 22, 2016 at 14:52
  • This particular application's purpose was to only load entities from a DB, do some post-processing (map entities to some DTOs) and publish the data via some REST endpoints. Totally read only, no business logic, that's why i skipped the service layer altogether in this case. Apr 21, 2016 at 11:02
-1

By adding a single bean definition the Spring container will act as a JPA container and inject an EnitityManager from your EntityManagerFactory.

see https://spring.io/blog/2006/08/07/using-jpa-in-spring-without-referencing-spring

1
  • That's unfortunately not an answer to this question - i already had PersistenceContext working, the only problem was the transaction handling Dec 7, 2016 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.