EJB is not hard, O/R mapping is


EJB is not hard, O/R mapping is

I weary of people constantly complaining about how hard EJB is and then coming up with their own thing like Hibernate or JDO or whatever. Any technology with similar features as EJB will be complicated. But it just happens that EJB can be made pretty easy if you have some tools.

I am going to talk about using EJB only in the context of WebLogic since that’s what I know. Maybe its harder on other application servers, it’s certainly part of our market advantage to be easier. I’ll go through the whole process of writing new application for WebLogic using just Ant, Emacs, and our command-line tools. All of this is much easier in Workshop, but I know that most people reading blogs probably aren’t using Workshop.

First things first, we are going to start from a WebLogic installation and MySQL and thats it. I am going to forgo all the special scripts and funny stuff thats included to cut directly to the bone. All you need to know is your BEA_HOME directory (I am using a Mac, so this may be different on your Gates machine):
 
export CLASSPATH=$BEA_HOME:$BEA_HOME/weblogic81/server/lib/weblogic.jar:mysql.jar
mkdir -p ~/domains/ejbexample
cd ~/domains/ejbexample
java weblogic.Server

Now you enter a username and password to create a server from nothing. It will put all the information you need into that directory and start up. Now go to another shell:

export CLASSPATH=$BEA_HOME:$BEA_HOME/weblogic81/server/lib/weblogic.jar
mkdir -p ~/apps/ejbexample/Example/KeywordEJB/com/sampullara/ejbs
cd ~/apps/ejbexample
cat > Example/KeywordEJB/com/sampullara/ejbs/KeywordBean.ejb
package com.sampullara.ejbs;

import javax.ejb.*;
import weblogic.ejb.*;

/**
 * @ejbgen:entity
 * ejb-name = Keyword
 * data-source-name = KeywordDS
 * table-name = keywords
 * prim-key-class = java.lang.Integer
 * default-transaction = Required
 *
 * @ejbgen:jndi-name
 * local = KeywordHome
 *
 * @ejbgen:finder
 * signature = “Keyword findKeywordByName(java.lang.String name)”
 * ejb-ql = “SELECT OBJECT(k) FROM Keyword AS k WHERE k.name = ?”
 */
public abstract class KeywordBean extends GenericEntityBean {
 
 /**
 * @ejbgen:cmp-field column = id
 * @ejbgen:primkey-field
 * @ejbgen:local-method
 */
 public abstract Integer getId();
 public abstract void setId(Integer id);

 /**
 * @ejbgen:cmp-field column = name
 * @ejbgen:local-method
 */
 public abstract String getName();
 /**
 * @ejbgen:local-method
 */
 public abstract void setName(String name);

 public Integer ejbCreate(Integer id) {
 setId(id);
 return id;
 }
 public void ejbPostCreate(Integer id) {}
}
<ctrl-D>

This will create an Ant build.xml file for us that gives us all the targets we need to make descriptors, build, and deploy our application to the server:

java weblogic.ant.taskdefs.build.BuildXMLGen -username [username] -password [password] -d . Example

The username and password were what you typed into the server when you started it. Now we build the descriptors needed for the EAR, and build the bean:

ant descriptors build

This gives you an application you can deploy by typing:

ant deploy

But we won’t be able to do that yet because we don’t yet have that data source in the server. The easiest way to make one is to just navigate to the console and add one to the server. I’m going to use MySQL for this, so I use this line to make the database table we need, on most commercial databases like Oracle the table will be created for you:

create table keywords (id int primary key, name varchar(30));

And the parameters for the database driver are mostly filled in for me in the console to make the pool, then I just make a datasource named ‘KeywordDS’ and associate it with the new pool. One might note that this entire process can be done in Workshop by creating an EJB project and adding a bean to it created from the database table. You will get essentially the same EJB code out from that.

Oh, what do you know, I didn’t end up using Emacs.