Java – JBoss as 7 cannot inject resource using @ persistencecontext_ Local container managed entitymanagers
I am using JBoss as 7.1 1, and you can configure a new JTA data source and connect it to my EJB
@PersistenceContext(unitName="TestPU") private EntityManager entityManager;
When I try to use resource_ When using local persistenceunit, I receive an error saying that I cannot inject resource using @ persistencecontext_ LOCAL PU.
I have configured persistence XML, as follows:
<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>
And in my Dao,
@Stateless public class UserDAO { @PersistenceContext(unitName="TestPU") private EntityManager entityManager; }
When I was in as 7.1 When deploying my application on 1, I received the following error
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
Use resource for any solution_ Local Pu and @ persistencecontext?
Solution
JTA: in the Java EE environment, transactions are handled by the container & its JTA transactions by default You can get the entity manager by lookup or injection
RESOURCE_ Local: in Java se, the application must explicitly manage transactions & Resource local transactions are local transactions You must create an entitymanagerfactory & then you can create an entity manager from it
When you deploy it in the application server, change the transaction type to persistence JTA. XML