Saturday, March 1, 2008

EJB Primer with WSAD - Part 3


Click on the images to view them full size!


Step 8 - Create the Session Facade

We will now create our Stateless Session Bean which will act as our Session Facade, an entry point to invoke our entity beans.

Right Click the EmployeeEJB project in the J2EE Perspective and click New->Enterprise Bean. Select the EJB Project as EmployeeEJB and select SessionBean in the radio. Give the bean name as EmployeeSession. The package would be employee.ejb by default. Lave it as such.



In the next screen, keep all defaults(Stateless is the session type and Transaction type is Container) and click Finish.

In the Project Navigator window, double click the EmployeeSessionBean.java to open the file in Java Editor.



Add the following line of code. This creates a protected member of type EmployeeEntity in the session bean.


protected EmployeeEntity employeeEntity = null;


Now, add a getter method to EmployeeSessionBean.java, that returns employeeEntity.


protected EmployeeEntity getEmployeeEntity(int id)
{

try
{
InitialContext initialContext = new InitialContext();
Object homeObject = initialContext.lookup("ejb/employee/ejb/EmployeeEntityHome");
EmployeeEntityHome employeeEntityHome =
(EmployeeEntityHome) javax.rmi.PortableRemoteObject.narrow(
homeObject,EmployeeEntityHome.class);
try {
System.out.println("Attempting to find entity, id=" + id);
employeeEntity =
employeeEntityHome.findByPrimaryKey(new EmployeeEntityKey(id));
} catch (FinderException e) {
System.err.println(
"Entity not found for id=" + id + ", FinderException occured: " + e);
e.printStackTrace();
} catch (Exception e) {
System.err.println("General Exception occured: " + e);
employeeEntity = null;
e.printStackTrace();
}
}
catch (Exception e)
{
System.err.println("General Exception occured: " + e);
employeeEntity = null;
e.printStackTrace();
}
return employeeEntity;
}


Also, add these import statements to the EmployeeSessionBean.java file.


import javax.ejb.*;
import javax.naming.*;


and add the following methods, to get and set the entity fields. Please note that none of the Entity Bean getters are accessible to the client in this case. The single window to access the entity bean fields is now, the EmployeeSessionBean class only.


public String getFirstName(int id) throws java.rmi.RemoteException
{
employeeEntity = getEmployeeEntity(id);
return employeeEntity.getFirstname();
}
public void setFirstName(int id, String firstName)throws java.rmi.RemoteException
{
employeeEntity = getEmployeeEntity(id);
employeeEntity.setFirstname(firstName);
}
public String getLastName(int id) throws java.rmi.RemoteException
{
employeeEntity = getEmployeeEntity(id);
return employeeEntity.getLastname();
}
public void setLastName(int id, String lastName)
throws java.rmi.RemoteException
{
employeeEntity = getEmployeeEntity(id);
employeeEntity.setLastname(lastName);
}


Now, promote these 4 methods to the Remote Interface, thus making it visible for the client through the Remote Interface. See the Screenshot below:



Step 9 - Bind CMP to the JNDI Name

Remember, we have already mapped our EmployeeEntity to an Oracle Database table. We also have our server configured to establish connection to the database by means of a DataSource. We also gave a JNDI name to this datasource - jdbc/EmployeeDS.

Now, we need to bind our entity bean to use this datasource. In the J2EE perspective, open the Project Navigator, and double click the ejb-jar.xml file under META-INF directory. This will open the visual configuration for ejb-jar.xml. Alternatively, you may click on the Source tab to view and edit the actual XML.



Scroll to the bottom of the page, where you will find the JNDI - CMP Connection Factory Binding configuration. Change the JNDI name as jdbc/EmployeeDS and set Container Autherization Type to Container.



Step 10 - Generate Deployment and RMIC Code

We are done with EJB coding (for our simple example). Now, it's time for us to request the IDE to generate the deployment code for our project.

In J2EE Hirarchy view, right click EmployeeEJB project->Generate->Deployment and RMIC Code.Make sure that both EmployeeEntity and EmployeeSession beans are selected. Click Finish



Go to : Part 1Part 2Part 3Part 4Part 5Part 6

No comments: