我曾两天试图在jboss AS 7中注入Entitymanager, 我已经创建了数据源java:jboss / datasources / PinDS在jboss控制台连接成功这里是我的
persistence.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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="MyAppUnit" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/PinDS</jta-data-source>
<!-- com.testing.Action -->
<class>com.testing.Action.GetENT</class>
<!-- Hibernate properties -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<property name="hibernate.validator.apply_to_ddl" value="false"/>
<property name="hibernate.validator.autoregister_listeners" value="false"/>
<property name="hibernate.show_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
<display-name>LC</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/index.jsp</location>
</error-page>
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>java:jboss/datasources/PinDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
EJB类
package com.testing.Action;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
/**
* Session Bean implementation class GetENT
*/
@Stateless
@LocalBean
public class GetENT {
@PersistenceContext(unitName="MyAppUnit")
private EntityManager em; //em is null tried @PersistenceUnit also
/**
* Default constructor.
*/
public GetENT() {
// TODO Auto-generated constructor stub
}
public void get(){
try{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyAppUnit"); //emf is gets created
System.out.println(emf1);
}catch(Exception w){
w.printStackTrace();
}
}
}
注意:
使用EntityManagerFactory emf=Persistence.createEntityManagerFactory("MyAppUnit");
我得到了emf,
但是,使用@PersistenceContext(unitName="MyAppUnit")
,emf为空。我错过了配置中的任何内容吗?