Advertisment

Persistence Storage with EJB 

author-image
PCQ Bureau
New Update

In the last part we showed how to develop and deploy stateless session bean using the WebLogic platform. A J2EE application is made up of session and entity beans. This article will demonstrate the container-managed entity bean. As entity bean stores the information in persistent storage, our application will use the PointBase database bundled with the WebLogic platform. 

Advertisment

The application



An entity bean consists of two interfaces (remote and local), bean class, three deployment descriptor files and database. A client application will store information in database and then retrieve it. We will develop an application for bank employees with three fields: ID, name and department that will be stored in database. The database will have a three-column table to store information. The application has to be developed according to J2EE specifications for EJB 2.0. The code for it is given on this month's PCQEssential CD.

The remote interface



Our remote interface is called Bank and defines three business methods: getName, getId and getDepart. These methods will return the employee name, employee id and department. The implementation of these methods is in bean class. 

Direct Hit!
Applies to:

Java developers (beginners/intermediate)
USP: Use BEA WebLogic to develop and deploy Entity Bean applications
On PCQEssential CD:

systems\labs\weblogic
Advertisment

package com.bank;



.


public interface Bank extends EJBObject {


public String getName() throws RemoteException;


public String getId() throws RemoteException;


public String getDepart() throws RemoteException;


}




The home interface



The home interface defines a method by which the client can create the bean (bean class). The interface has an additional method 'findByPrimaryKey' as compared to the session bean. The method helps to retrieve the employee data from the database. It throws two exceptions called finder and remote. Finder exception will be thrown if there is an error in retrieving the data from the database. The interface has optional finder methods for querying the database and fetching the data. 

import javax.ejb.EJBHome;



.


public interface BankHome extends EJBHome {


public Bank create(String name,String id,String depart) throws CreateException,RemoteException;


public Bank findByPrimaryKey(String primaryKey) throws FinderException,
RemoteException;







Advertisment

The bean



The bean class must meet the container managed persistence syntax requirements. First, the class must be defined as public and abstract, must implement the 'ejbCreate' method of the home interface, as well as the 'get and set access' methods must be defined as public and abstract for persistence. The get and set methods should be there for every field in the database. Our bank application has three fields; hence we have three get and set access methods. The bean class also has empty methods such as ejbPostCreate() and ejbActivate() as required by the J2EE specifications. 

import javax.ejb.CreateException;



import javax.ejb.DuplicateKeyException;


abstract public class BankBean implements EntityBean {


.


public void setEntityContext(EntityContext entityContext) {


.


abstract public String getName();


abstract public void setName(String name);


abstract public String getId();


abstract public void setId(String id);


abstract public String getDepart();


abstract public void setDepart(String depart);


.


public String ejbCreate(String name,String id,String depart) throws CreateException {


setName(name);


setId(id);


setDepart(depart);


}


.


}


public void ejbPostCreate(String name,String id,String depart) {}


public void ejbActivate() {}


.


}





















The client



The client inserts employee data in the database through createAccount method. The employee data is defined in three String arrays. We pass an employee ID to the findByPrimaryKey method to retrieve the employee name. The JNDI name for our application is

bank_april. 

Advertisment

package com.bank;



import javax.rmi.PortableRemoteObject;


public class Client


{


String <> name = {"sam","ram","gyan","rishi"};


String <> id = {"100","101","102","103"};


String <> dep = {"loan","credit","product","personal"};


Bank bs = (Bank) PortableRemoteObject.narrow(home. findByPrimaryKey("100"), Bank.class);


System.out.println("100 id name is" + bs.getName( ));


}







private Bank createAccount(String name, String id, String depart)throws



CreateException,RemoteException


{


Bank ac = (Bank) PortableRemoteObject.narrow(home.create( name,id,depart),Bank.class);


return ac;


}





home = (BankHome)ctx.lookup("bank_april");


.


}







Creating database and data source



The next step is to create a PointBase database and data source associated with it. For this, go to C:\bea\weblogic81\common\bin. Double click on 'startPointBase' script icon. PointBase server will be started and a message will be displayed in the command prompt 'server started...'. Double click on the 'startPointBaseConsole' script icon to launch the console. Enter the following in the console.

Advertisment

Driver: com.pointbase.jdbc.jdbcUniversalDriver



URL: jdbc:pointbase:server://localhost:9093/server


User:weblogic


Password:weblogic

Select the 'Open specified database option' and click on OK. On the left hand side select Schemas>WebLogic. Right click on Tables>Create Table. Then click on Next and give 'bank' as the table name. Again click on Next. Now three columns: id, name and departmeant are to be created. So click on Add. Under Column name enter 'id', SQL Type 'VARCHAR', length '20' and check off the 'Allow nulls' option. Click on Add and enter Column name 'id', SQL Type 'VARCHAR' and length '20'. Click on Add and enter Column name 'depart', SQL Type 'VARCHAR' and length '20'. Click on Next three times on the consecutive screens and then on Finish to create the table 'bank'. This step involves creation of the database connection pool. Start the WebLogic server as discussed in the previous part and login in to the console. Click on Services>Connection Pools on the left-hand side of console and select 'Configure a new JDBC connection pool'. Select database type 'PointBase', database driver 'PointBase's Driver(Type4) Versions:4.X'. Click on 'Continue and note the JDBC connection pool name'. Enter Database Name 'server', Host Name 'local host', port '9093', username 'weblogic' and password 'weblogic'. Click on Continue and then on the 'Test Driver Configuration' button. A 'Connection successful' message will be displayed if everything is ok. Next click on the 'create and deploy' button to create the connection pool. The next step is to create data source for the connection pool. Click on Services>Data sources on the left-hand side of the console and select 'Configure a new JDBC Data source'. Call data source 'ds' and JNDI as 'bankDS'. Click on Continue. Select the connection pool name as given above and click on Continue. Now click on Create. A data source with JNDI name 'bankDS' will be created.

Compiling and deploying the application

Advertisment

- You need to set the class path as:

set classpath = c:\bea\weblogic81\server\lib\weblogic.jar;c:\bank;%classpath% 

- Create a folder called 'bank' in C:\ and put our Bank, BankHome and BankBean JAVA files in it. From the command prompt, compile the JAVA files and create the deployment JAR file using

C:\> javac -d . *.java



C:\> jar -cvf bank.jar com

- Now you need to create deployment descriptor files using the WebLogic Builder. WebLogic Builder automatically creates three deployment descriptor files, namely weblogic-ejb-jar.xml, ejb.jar.xml and weblogic-cmp-rdbms-jar.xml. These files contain information about the application and WebLogic specific environment properties. Discussing in detail about the file is beyond the scope of this article. 

- Now open the WebLogic Builder and open this bank.jar (bank.jar is created in bank folder) file by clicking on File>Open. When prompted, select Yes to create new descriptors. From the left-side pane, select BankBean. Select the General tab on the right-hand side and give JNDI name as bank_april. Click on the Classes tab, select primary key class 'java.lang.String', as the home interface has primary key as a String. Select primary key field 'id'. Under Container Managed Persistence, enter Datasource name as 'bankDS' (name of JNDI data source given above) and Table name as 'bank'. 

Now, from the menu, go to Tools>Validate Descriptors. It's fine if you get an 'EJBC Successful' message, otherwise you need to rectify the error in JAVA files. Click on File>Save and close the Weblogic Builder.

- Now the bank.jar is to be deployed in the WebLogic server. Select Deployments>EJB Modules on left-hand side of console. Select 'Deploy a new EJB module', browse to C:\bank and then open the bank.jar file. Select the 'Target Module' button and then Deploy. 

Running the client



Copy the Client.java file to the 'bank' folder and run

C:\> javac - d . Client.java



C:\> java com.bank.Client

You can create similar applications with a number of database fields and insert records. In the next issue we will delve deeper into entity bean and do database queries. 

Sushil Oswal

Advertisment