Reputation: 951
I'm trying to inject EntityManager with @PersistenceContext annotations to my EJB but i keep getting NULL EntityManager
What am I doing wrong?
Calling to ejb from struts action
This my code: (the problem is in class TaPaymentInfoBean.java in the bootom of my post)
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>
War side:
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 side:
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:(where EntityManager is null)
public class TaPaymentInfoBean implements javax.ejb.SessionBean {
@PersistenceContext(unitName="BookingUnit")
private EntityManager em;
}
public PaymentInfoContainer getAllPaymentInfo(PaymentInfoSearch paymentInfoSearch){
em.find(PaymentInfo.class, paymentInfoSearch.getKey);
}
My server log looks just fine with no error and when I try this code it works just fine
EntityManagerFactory emf = Persistence.createEntityManagerFactory("BookingUnit");
em = emf.createEntityManager();
But the injection needs to work too.
Upvotes: 0
Views: 1210
Reputation: 33956
Injection and persistence-context-ref are not supported in <ejb-jar version="2.0">
. Try using 3.0 with something like this:
<?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>
Upvotes: 3