我创建了一个PersonDao对象,它是一个应该使用注入的实体管理器的可注入bean。问题是@PersistenceContext()不会注入我的对象并将其保留为null。我正在使用JBoss独立的eap 6.2。贝娄我是我的档案:
hello.dao / PersonDao的:
package hello.dao;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import hello.entity.Location;
import hello.entity.Person;
@Stateless
public class PersonDao {
@PersistenceContext(unitName="helloPersistence")
static EntityManager em;
Person person;
@Inject
public PersonDao() {
}
public void addLocation(Location location) {
// em is always null
System.out.println(em.toString());
em.persist(person);
em.persist(location);
person.getLocations().add(location);
location.getPersons().add(person);
em.flush();
}
public void setPerson(Person person) {
this.person = person;
}
public void savePerson() {
em.persist(person);
em.flush();
}
public static List<?> list() {
List<?> results = em.createNativeQuery("SELECT * FROM PERSON").getResultList();
return results;
}
}
的persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="helloPersistence">
<jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
<class>hello.dao.LocationDao</class>
<class>hello.dao.PersonDao</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="javax.persistence.schema-generation.database.action"
value="drop-and-create" />
</properties>
</persistence-unit>
</persistence>
Standalone.xml数据源:
<datasources>
<datasource jta="true" jndi-name="java:jboss/datasources/MySqlDS" pool-name="java:jboss/datasources/MySqlDS_Pool" enabled="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:mysql://localhost:3306/hello</connection-url>
<driver>mysql</driver>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>100</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>root</user-name>
<password>password</password>
</security>
</datasource>
<drivers>
<driver name="mysql" module="com.mysql.connectorj">
<driver-class>com.mysql.jdbc.Driver</driver-class>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
<driver name="db2" module="com.ibm.db2">
<driver-class>com.ibm.db2.jcc.DB2Driver</driver-class>
<xa-datasource-class>com.ibm.db2.jcc.DB2XADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
standalone.sh上的战争部署
13:19:39,530 INFO [org.jboss.as.server.deployment] (MSC service thread 1-14) JBAS015876: Starting deployment of "hello.war" (runtime-name: "hello.war")
13:19:39,871 INFO [org.jboss.as.jpa] (MSC service thread 1-14) JBAS011401: Read persistence.xml for helloPersistence
13:19:39,912 INFO [org.jboss.weld.deployer] (MSC service thread 1-11) JBAS016002: Processing weld deployment hello.war
13:19:39,915 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-11) JNDI bindings for session bean named PersonDao in deployment unit deployment "hello.war" are as follows:
java:global/hello/PersonDao!hello.dao.PersonDao
java:app/hello/PersonDao!hello.dao.PersonDao
java:module/PersonDao!hello.dao.PersonDao
java:global/hello/PersonDao
java:app/hello/PersonDao
java:module/PersonDao
13:20:07,809 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-9) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)
13:20:07,810 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-9) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.fabric.jdbc.FabricMySQLDriver (version 5.1)
13:20:07,810 INFO [org.jboss.weld.deployer] (MSC service thread 1-9) JBAS016005: Starting Services for CDI deployment: hello.war
13:20:07,813 INFO [org.jboss.as.jpa] (ServerService Thread Pool -- 436) JBAS011402: Starting Persistence Unit Service 'hello.war#helloPersistence'
13:20:07,814 INFO [org.jboss.weld.deployer] (MSC service thread 1-4) JBAS016008: Starting weld service for deployment hello.war
13:20:07,814 INFO [org.hibernate.ejb.Ejb3Configuration] (ServerService Thread Pool -- 436) HHH000204: Processing PersistenceUnitInfo [
name: helloPersistence
...]
13:20:07,823 INFO [org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator] (ServerService Thread Pool -- 436) HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
13:20:07,825 INFO [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 436) HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
13:20:07,829 INFO [org.hibernate.engine.transaction.internal.TransactionFactoryInitiator] (ServerService Thread Pool -- 436) HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
13:20:07,829 INFO [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (ServerService Thread Pool -- 436) HHH000397: Using ASTQueryTranslatorFactory
13:20:07,890 INFO [org.jboss.web] (ServerService Thread Pool -- 446) JBAS018210: Register web context: /helloMan
13:20:07,908 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018565: Replaced deployment "hello.war" with deployment "hello.war"
答案 0 :(得分:4)
我必须检查JPA规范,但我敢打赌容器根本不喜欢static
。你能尝试一下:
@PersistenceContext(unitName="helloPersistence")
private EntityManager em; // Not a static but an instance field
代替?