gpt4 book ai didi

java - PersistenceContext 为空

转载 作者:行者123 更新时间:2023-12-01 17:07:51 26 4
gpt4 key购买 nike

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

持久性.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 上的 War 部署

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"

最佳答案

我必须检查 JPA 规范,但我敢打赌容器根本不喜欢那个static。你能尝试一下吗:

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

相反?

关于java - PersistenceContext 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24619146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com