The Phone Book Bean Example

This is an example of a JSP-page calling an Enity Bean that uses annotations. The result looks like this:
The primary purpose of the example is to provide a very simple demonstration of an EJB Entity bean. It is not intended to be a complete example or one that could be used as a template for creating your own EJB Entity bean. This is an example using Geronimo 2.1, Java 1.5 and EJB 3.0.

Application Contents

First, let us take a look at the PhoneBook Entity Bean that represents a table in the database. Each instance of PhoneBook is a record of the table.PhoneBook.java uses the

@Entity annotation to mark this class as an Entity Bean

@Table annotation to map the table name that is being represented by the Entity Bean.

@Id annotation to specify the primary key of the table.

And, as usual there is an empty constructor for the Entity Bean

PhoneBook.java

MyPhonebookLocal.java is the business interface that drives the above mentioned Entity Bean.

MyPhonebookLocal.java

MyPhonebookBean.java is where the implementation of the local (and if there is, a the remote) interface exists. To explain what the annotations in this Stateless Session Bean means I will enumerate them:

@Stateless - tells Geronimo that this is a stateless session bean

@PersistenceUnit - tells Geronimo to retrieve a persistence unit defined in the persistence.xml and place it in the EntityManagerFactory

Note that PersistenceContext is used when you are directly obtaining a EntityManager. For an EntityManagerFactory use PersistenceUnit.

MyPhonebookBean.java

index.jsp is the JSP page that uses the EJB to access the database.

index.jsp

Deployment Plans

persistence.xml will specify the name of the PersistenceUnit. This name is used when referencing for the EntityManagerFactory. I have denoted it as PhonePU. I added an extra property called SynchronizeMappings so that the data in the database will not be overwritten.

persistence.xml

Deployment Plan for the Application

plan.xml is generated by building the sample and can be found under ./myphonebook-jetty/target/resources/META-INF/plan.xml to deploy on jetty or ./myphonebook-tomcat/target/resources/META-INF/plan.xml to deploy on tomcat following a successful build of the sample. Shown below is the deployment plan for tomcat.

plan.xml

Building, Configuring, and Deploying the Application

Source Code for Sample

Creating and Populating Database

If you choose to install the sample as a plugin it is not necessary to create or populate the database as this will be completed as part of the plugin installation. However, if you prefer to deploy the sample using the generated ear you must first install the sample-datasource plugin. You can use either the administration console plugin portlet or the command line install-plugin to install the sample-datasource. When you later deploy the sample the DBInitialization GBean included in the deployment plan will populate the database.

Building

Use a command prompt to navigate into the myphonebook directory and just give mvn install followed by mvn site command to build. It will create the myphonebook-ear-2.1-SNAPSHOT.ear under the myphonebook folder. Now, you are ready to deploy myphonebook application in the Geronimo Application server using either the plugins generated for tomcat and jetty or the deployment plan that was generated and the ear.

Deploying the Application

As previously mentioned, there are two different ways to deploy the sample application - deployment as a plugin or deployment using the ear and plan.

To deploy the sample as a plugin you should do the following using the Geronimo administration console:

Scroll down to Plugins from the Console Navigation panel.

Select Update Repository List to get the repository for the server release in use added

Select the repository for the geronimo release in use and the click on Show Plugins in selected repository

Scroll down to the myphonebook plugin for your Geronimo configuration (Jetty or Tomcat) and then select install

To deploy the sample using the ear do the following using the Geronimo administration console:

First, install the sample-datasource plugin. You can do this as follows:

Scroll down to Plugins from the Console Navigation panel.

Select Update Repository List to get the repository for the server release in use added

Select the repository for the geronimo release in use and the click on Show Plugins in selected repository

Scroll down to the myphonebook plugin for your Geronimo configuration (Jetty or Tomcat) and then select install

Scroll down to Deploy New from the Console Navigation panel.

Load myphonebook-ear-2.1-SNAPSHOT.ear from ./myphonebook-ear/target/ in to the Archive input box.

Load plan.xml from ./myphonebook-jetty/target/resources/META-INF/ or ./myphonebook-tomcat/target/resources/META-INF/ depending on the target server (jetty or tomcat).