我正在尝试向我的EJB注入带有@PersistenceContext注释的EntityManager,但我一直在获取NULL EntityManager
我做错了什么?
从struts行动中调用ejb
这是我的代码:(问题出在我帖子的bootom中的TaPaymentInfoBean.java类中)
的persistence.xml:
<persistence version="1.0" 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_1_0.xsd">
<persistence-unit name="BookingUnit" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>jdbc/payment</jta-data-source>
<class>dataobjects.PaymentInfo</class>
<class>dataobjects.PaymentRequest</class>
<class>dataobjects.Address</class>
<properties>
<property name="openjpa.Log" value="DefaultLevel=TRACE, Runtime=TRACE, Tool=TRACE, SQL=TRACE"/>
<property name="openjpa.TransactionMode" value="managed"/>
</properties>
<
</persistence-unit>
</persistence>
战争方面:
EJBdelegate.java
public PaymentInfoContainer getAllPaymentInfo(PaymentInfoSearch paymentInfoSearch) throws RemoteException, SQLException {
try {
ParamLookupManagerHome paramLookupHome = (ParamLookupManagerHome) EJBHomeFactory.getInstance().lookupHome("java:comp/env/paramLookupManagerNEWADMIN", ParamLookupManagerHome.class, true);
ParamLookupManager param = paramLookupHome.create();
return param.getAllPaymentInfo(paymentInfoSearch);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
}
EJB方面:
EJB-jar.xml中:
<session id="ParamLookupManager">
<ejb-name>ParamLookupManager</ejb-name>
<home>beans.ParamLookupManagerHome</home>
<remote>beans.ParamLookupManager</remote>
<ejb-class>beans.ParamLookupManagerBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<ejb-ref id="EjbRef_1111111111">
<ejb-ref-name>ejb/TaPaymentInfo</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>beans.TaPaymentInfoHome</home>
<remote>beans.TaPaymentInfo</remote>
<ejb-link>TaPaymentInfo</ejb-link>
</ejb-ref>
</session>
<session id="TaPaymentInfo">
<ejb-name>TaPaymentInfo</ejb-name>
<home>beans.TaPaymentInfoHome</home>
<remote>beans.TaPaymentInfo</remote>
<ejb-class>beans.TaPaymentInfoBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>TaPaymentInfo</ejb-name>
<method-intf>Remote</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
ParamLookupManager.java:
public interface ParamLookupManager extends javax.ejb.EJBObject {
public PaymentInfoContainer getAllPaymentInfo(PaymentInfoSearch paymentInfoSearch) throws RemoteException, SQLException;
}
ParamLookupManagerHome.java:
public interface ParamLookupManagerHome extends javax.ejb.EJBHome {
public beans.ParamLookupManager create()
throws javax.ejb.CreateException, java.rmi.RemoteException;
}
ParamLookupManagerBean.java:
public class ParamLookupManagerBean implements javax.ejb.SessionBean {
public PaymentInfoContainer getAllPaymentInfo(PaymentInfoSearch paymentInfoSearch) throws SQLException {
TaPaymentInfoHome taPaymentInfoHome = (TaPaymentInfoHome)TAHomeFactory.getInstance().getEJBHome(TAHomeFactory.TAPAYMENTINFO_HOME_NAME);
TaPaymentInfo taPaymentInfo = taPaymentInfoHome.create();
PaymentInfoContainer lst = taPaymentInfo.getAllPaymentInfo(paymentInfoSearch);
return lst;
}
}
TaPaymentInfo.java:
public interface TaPaymentInfo extends javax.ejb.EJBObject {
public PaymentInfoContainer getAllPaymentInfo(PaymentInfoSearch paymentInfoSearch) throws RemoteException;
}
TaPaymentInfoHome.java:
public interface TaPaymentInfoHome extends javax.ejb.EJBHome {
public beans.TaPaymentInfo create()
throws javax.ejb.CreateException, java.rmi.RemoteException;
}
TaPaymentInfoBean :(其中EntityManager为空)
public class TaPaymentInfoBean implements javax.ejb.SessionBean {
@PersistenceContext(unitName="BookingUnit")
private EntityManager em;
}
public PaymentInfoContainer getAllPaymentInfo(PaymentInfoSearch paymentInfoSearch){
em.find(PaymentInfo.class, paymentInfoSearch.getKey);
}
我的服务器日志看起来很好,没有错误,当我尝试这个代码时,它工作得很好
EntityManagerFactory emf = Persistence.createEntityManagerFactory("BookingUnit");
em = emf.createEntityManager();
但注射也需要起作用。
答案 0 :(得分:3)
<ejb-jar version="2.0">
不支持注入和持久性context-ref。尝试使用3.0这样的东西:
<?xml version="1.0"?>
<ejb-jar
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
version="3.0"
>
<enterprise-beans>
...
</enterprise-beans>
</ejb-jar>