我正在尝试在JBoss AS 6上的JEE 6中设置一个持久性单元。
注入的EntityManager始终为null。我已经摆弄了相当多的配置,但无法得到任何工作。我已经尝试过JTA / LOCAL_RESOURCE,我尝试在我的代码中使用与我的persistence.xml相同的单位名称,但也使用前面的war名称,因为日志表明JNDI名称可能就是这样。
我有一个mysql-ds.xml文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>myconnection</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/cog1</connection-url>
<driver-class>org.gjt.mm.mysql.Driver</driver-class>
<user-name>bw</user-name>
<password></password>
<min-pool-size>2</min-pool-size>
<max-pool-size>20</max-pool-size>
<idle-timeout-minutes>5</idle-timeout-minutes>
<exception-sorter-class-name>com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter</exception-sorter-class-name>
<valid-connection-checker-class-name>com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker</valid-connection-checker-class-name>
</local-tx-datasource>
</datasources>
我有一个persistence.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/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">
<persistence-unit name="myunit" transaction-type="RESOURCE_LOCAL">
<jta-data-source>java:/myconnection</jta-data-source>
<non-jta-data-source>java:/myconnection</non-jta-data-source>
<class>com.cognitura.simulation.dao.model.History</class>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.logging.level" value="INFO" />
</properties>
</persistence-unit>
</persistence>
我有一个带有以下代码的bean:
@Stateful
@LocalBean
public class HistoryEJB {
@PersistenceContext(unitName = "myunit")
private EntityManager em;
@EJB
private RealityElement el;
我可以看到RealityElement el被注入正常,所以我认为它肯定是容器管理的?
感谢任何想法!
编辑:日志似乎可以设置'myunit'单元。有一个警告,但我读到它并不重要?
01:28:06,276 INFO [org.jboss.jpa.deployment.PersistenceUnitDeployment] Starting persistence unit persistence.unit:unitN
ame=cog1.war#myunit
01:28:06,278 INFO [org.hibernate.ejb.Ejb3Configuration] Processing PersistenceUnitInfo [
name: myunit
...]
01:28:06,305 WARN [org.hibernate.ejb.Ejb3Configuration] Persistence provider caller does not implement the EJB3 spec co
rrectly.PersistenceUnitInfo.getNewTempClassLoader() is null.
这里看起来很开心:
01:28:06,965 INFO [org.hibernate.impl.SessionFactoryObjectFactory] Bound factory to JNDI name: persistence.unit:unitNam
e=cog1.war#myunit
答案 0 :(得分:0)
这可能是jndi的名字吗?尝试在数据源和持久性文件中进行以下更改
<jndi-name>java:jboss/datasources/employeedb</jndi-name>
<jta-data-source>java:jboss/datasources/employeedb</jta-data-source>