I have a Spring MVC 4.0 application, and I am learning JPA. I use Hibernate as the JPA implementation.
I can configure Hibernate as described in this tutorial. It works fine, but I have to use Hibernate's Session
object:
@Autowired
SessionFactory sessionFactory;
...
Session session = sessionFactory.openSession();
Now, I want to use JPA's EntityManager
instead. I have followed this tutorial on the same web site (the configuration is very similar). And I tried to obtain an EntityManager
object this way:
@PersistenceContext
EntityManager entityManager;
I got a runtime message:
java.lang.IllegalStateException: No transactional EntityManager available
Then, I followed the suggestion in this answer, and tried to use the following code:
@PersistenceContext
EntityManager entityManager;
...
entityManager=entityManager.getEntityManagerFactory().createEntityManager();
It works a few times (about 9 repetitive method invocations), and then the application freezes.
What is the right way to get EntityManager
in Spring + Hibernate configuration?
I do not need any Spring transaction functionality for now. I just want to get an access to EntityManager
and play with JPA.
Spring/Hibernate configuration file (hibernate.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: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">
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test_db" />
<property name="username" value="test" />
<property name="password" value="test" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="net.myproject" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<tx:annotation-driven />
</beans>
The class where I attempt to use EntityManager
@Repository
public class ProductsService {
@PersistenceContext
EntityManager entityManager;
@Transactional
public GridResponse<Product> getProducts(GridRequest dRequest) {
// The following line causes the exception: "java.lang.IllegalStateException: No transactional EntityManager available"
Session session = entityManager.unwrap(Session.class);
//...
}
...