How to inject an EJB into an EJB???
843830Jun 1 2006 — edited Oct 16 2006I developed a simple J2EE Project using Netbeans 5.5.
I have the following EJB's:
- ShoppingCartBean.java (Stateful Session Bean)
- ShoppingCartLocal.java
- Product.java (Entity Bean)
- ProductFacade.java
- ProductFacadeLocal.java
The ShoppingCartBean.java has the basic structure:
@Stateful
public class ShoppingCartBean implements ShoppingCartLocal {
@EJB
private ProductFacadeLocal productFacade;
...
public void addProduct( String id ) {
Product product = productFacade.find( id );
...
}
...
}
HOWEVER, when I try and run the app, I get the following error:
Deploying application in domain failed; Error loading deployment descriptors for module [help2] -- This bean [ShoppingCartBean] has no ejb reference by the name of [productFacade]
I believe that the error is caused by the file < sun-ejb-jar.xml >. Here are the contents of that file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
<sun-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>ShoppingCartBean</ejb-name>
<ejb-ref>
<ejb-ref-name>productFacade</ejb-ref-name>
<jndi-name>ejb/productFacade</jndi-name>
</ejb-ref>
</ejb>
</enterprise-beans>
</sun-ejb-jar>
When I change the <sun-ejb-jar.xml> file to the following:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
<sun-ejb-jar>
<enterprise-beans/>
</sun-ejb-jar>
THEN, I get a null pointer exception for the statement:
Product product = productFacade.find( id );
Obviously, I am not doing this correctly??? What am I doing wrong? The <sun-ejb-jar.xml> file is automatically generated by Netbeans.
Strangely, I can inject an @EJB reference into a servlet with NO Problems!!! Also, when I inject an @EJB reference into a servlet, the sun-ejb-jar.xml file is not altered in any way???
CAN YOU PLEASE WALK ME THROUGH WHAT I'M DOING WRONG???
Do you know of any working examples where an @EJB reference is successfully injected into another EJB???