Wednesday, December 05, 2007

Accessing EJBs 2.0 in ORACLE JDeveloper 10.1.3.3.0

After creating your EJBs in JDeveloper 10.1.3.3.0, you need to use it to invoke your business logic in case of Session Beans or query the database in case of Entity Beans. Creation itself if somehow easy, we can take a quick overview about:
*select project you want EJBs , from menu File-->New-->Business Tier-->EJB
*Choose type of bean you need and follow wizard to complete creating your bean

Accessing EJB need to lookup it through JNDI APIs like following:

try{
Context c;
c = new InitialContext();
tstSessionEJBLocalHome creator = (tstSessionEJBLocalHome)c.lookup("localRefName");
tstSessionEJBLocal bean;
bean = creator.create();
result = bean.businessMethod1(parameter_list);
}
catch(NamingException ne){
result = "Naming Error";
}
catch(CreateException ce){
result = "Creation Error";
}

sure the above code will differ when you access the remote interface of the bean, but the key point here if JNDI name "localRefName" which we need to lookup the bean.

By default, you can use tstSessionEJB in the deployment descriptor ejb-jar.xml to lookup for remote home interface of Session Bean.

Generally speaking you need to add ejb-ref for looking up remote bean and ejb-local-ref for looking up local bean. This reference should be added in scope you need to call EJB within it.

May be the above words are so vague, I think following examples will be more OK.

Example1:
If you need to call local Entity Bean from a Session Bean, you need to add ejb-local-ref to the desired Entity Bean in EJB deployment descriptor scope inside this Session Bean tag like following



and then a method private EmployeeLocalHome getEmployeeLocalHome(); will be auto-generated by JDeveloper which you can use to call Entity Bean you need. Note that auto-generation of the method will occur if you use the wizard like described below.

Example2:
If you need to call remote Session Bean from a Servlet, you need to add ejb-ref in web.xml which is the scope of the site (scope serlvets work on) like following:



Now you can use ejb-ref-name to lookup EJB through JNDI APIs mentioned above.

Finally we want to know how to create these ejb-ref tags. If you need to add an ejb-ref to a called Bean in an existing caller Bean:
- double click the caller bean in the left handed Application Navigator of JDeveloper
- choose from the left handed bar "EJB References" or "EJB Local References" according what you need to call
- click Add button in the right and fill called bean info you asked for then OK

in case you need to add an ejb-ref to web.xml:
- Open web.xml
- Drag and Drop from right handed "Component Palette" ejb-ref or ejb-local-ref according bean you need to call
- fill called bean info you asked for then OK

Also you can use generated generated ejb-ref-name to lookup for bean you want.

Hope this helps

No comments: