Java 没有可用的事务 EntityManager - 使用 JPA Api,Hibernate Session 出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22382950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No transactional EntityManager available - working with JPA Api, error with Hibernate Session
提问by Michal Boska
I am trying to unwrap the Hibernate Session from injected EntityManager, as I need to use Hibernate's native Criteria API.
我正在尝试从注入的 EntityManager 中解开 Hibernate Session,因为我需要使用 Hibernate 的本机 Criteria API。
When I try to use Criteria i get following exception:
当我尝试使用 Criteria 时,出现以下异常:
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.
但是,当我使用 EntityManager 本身的 Query api 时,一切正常。
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:
PouzivatelDAOHibernate 不起作用:
@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:
可以工作的 PouzivatelDAOHibernate:
@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
编辑:我正在使用 Spring Data JPA
采纳答案by Michal Boska
I've actually solved the problem (the solution was pretty trivial): use org.springframework.transaction.annotation.Transactional
instead of javax.transaction.Transactional
我实际上已经解决了这个问题(解决方案非常简单):使用org.springframework.transaction.annotation.Transactional
而不是javax.transaction.Transactional
回答by Koitoer
Consider using @Transactional
over your DAO methods. It seems that your configuration is correct.
考虑使用@Transactional
你的 DAO 方法。看来你的配置是正确的。
回答by Surya
By adding a single bean definition the Spring container will act as a JPA container and inject an EnitityManager from your EntityManagerFactory.
通过添加单个 bean 定义,Spring 容器将充当 JPA 容器并从您的 EntityManagerFactory 注入一个 EnitityManager。
see https://spring.io/blog/2006/08/07/using-jpa-in-spring-without-referencing-spring
见 https://spring.io/blog/2006/08/07/using-jpa-in-spring-without-referencing-spring