Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java PersistenceContext为空_Java_Hibernate_Jakarta Ee_Jboss - Fatal编程技术网

Java PersistenceContext为空

Java PersistenceContext为空,java,hibernate,jakarta-ee,jboss,Java,Hibernate,Jakarta Ee,Jboss,我创建了一个PersonDao对象,它是一个应使用注入实体管理器的可注入bean。问题是@PersistenceContext()没有注入我的对象,并将其保留为null。我使用的是JBoss单机版EAP6.2。下面是我的文件: hello.dao/PersonDao: package hello.dao; import java.util.List; import javax.ejb.Stateless; import javax.inject.Inject; import javax.pe

我创建了一个PersonDao对象,它是一个应使用注入实体管理器的可注入bean。问题是@PersistenceContext()没有注入我的对象,并将其保留为null。我使用的是JBoss单机版EAP6.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;
    }
}

我必须检查JPA规范,但我打赌容器根本不喜欢那种
静态的
。你能试试吗

@PersistenceContext(unitName="helloPersistence")
private EntityManager em; // Not a static but an instance field

相反?

我必须检查JPA规范,但我敢打赌容器根本不喜欢
静态的
。你能试试吗

@PersistenceContext(unitName="helloPersistence")
private EntityManager em; // Not a static but an instance field

而是?

静态EntityManager?静态EntityManager?
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"
@PersistenceContext(unitName="helloPersistence")
private EntityManager em; // Not a static but an instance field