With a stateless session bean under our belt, a simple entity bean logically follows. This example mirrors the OpenEJB CMP entity bean example provided by the OpenEJB team. The database changes from PostgreSQL to HSqlDB. And the ant build file incorporates a custom OpenEJB configuration, automated entity bean deployment, and both local and remote execution.
Let’s start with the mapping file. The example is self-explanatory: specify the primary key or identity value along with the insertion key generation method, map the three bean member variables to database elements, and declare types for each mapping pair. This example uses the HSqlDB identity for key generation, where the OpenEJB example used a PostgreSQL sequence. Take a look at the Castor DTD and the Castor JDO key generator support for more information on writing a mapping file.
The remaining configuration files mirror the OpenEJB entity bean example. Note that the included configuration files use absolute path names and must be modified for your installation. All the configuration files are located in the ./src/conf/ directory.
There are only slight changes to the ant build file for this example. We explicitly copy the jar file to the deployments directory and execute the deploy method in place. This works around surprising deploy copy method behavior when using a custom bean deployments directory. The new deploy target is shown below. Note that an input file has been specified for the openejb deploy method – the input file provides the query for a Home findByName method.
<target name="deploy" depends="jar,deploy-check" unless="deploy.notNeeded">
Here is the Home interface definition. The findByPrimaryKey method is automatically implemented while the findByName method requires that we define a query when deploying the bean (as described in the previous paragraph).
Exercise the entity bean with this simple driver method. It simply searches for a keyword bean named openejb. If the bean exists, then we remove it. And if it does not exist, then we create it.
Sample output from two local executions. The first execution creates the bean and the second removes it.
$ ant local Buildfile: build.xml init: compile: jar: deploy-check: deploy: local: [java] OpenEJB 0.9.2 build: 20030605-0409 [java] http:/\/openejb.sf.net [java] Create keyword openejb BUILD SUCCESSFUL Total time: 27 seconds $ ant local Buildfile: build.xml init: compile: jar: deploy-check: deploy: local: [java] OpenEJB 0.9.2 build: 20030605-0409 [java] http:/\/openejb.sf.net [java] Remove: openejb-an EJB container BUILD SUCCESSFUL Total time: 18 seconds
Disclaimer: I don’t claim to be an expert on OpenEJB. Please send comments and corrections.