我正在使用JBoss AS 7.1.1,并且可以配置一个新的JTA数据源并将其连接到我的EJB
@PersistenceContext(unitName="TestPU") private EntityManager entityManager;
当我尝试使用RESOURCE_LOCAL PersistenceUnit时,我收到错误,说我无法使用@PersistenceContext注入RESOURCE_LOCAL PU.
我已经配置了persistence.xml,如下所示:
<persistence-unit name="TestPU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="javax.persistence.jdbc.driver" value="com.MysqL.jdbc.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:MysqL://localhost:3306/xy"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="blah"/> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit>
而在我的DAO中,
@Stateless public class UserDAO { @PersistenceContext(unitName="TestPU") private EntityManager entityManager; }
当我在AS 7.1.1上部署我的应用程序时,我收到以下错误.
JBAS011428: Cannot inject RESOURCE_LOCAL container managed EntityManagers using @PersistenceContext at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor$1.handle(ModuleJndiBindingProcessor.java:169) at org.jboss.as.ee.component.ClassDescriptionTraversal.run(ClassDescriptionTraversal.java:54) at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.processClassConfigurations(ModuleJndiBindingProcessor.java:162) at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.deploy(ModuleJndiBindingProcessor.java:155) at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final] ... 5 more
任何解决方案使用RESOURCE_LOCAL PU与@PersistenceContext?
解决方法
JTA:在Java EE环境中,事务由容器&默认情况下其JTA事务.您可以通过查找或注入获取实体管理器.
RESOURCE_LOCAL:在Java SE中,应用程序必须明确管理事务&资源本地事务是本地事务.你必须创建EntityManagerFactory&那么可以从它创建实体管理器.
当您在应用程序服务器中部署它时,请将事务类型更改为persistence.xml中的JTA.