EJB 2 – Entity EJB with xDoclet, MyEclipse, Jboss and PostgreSql
Creation
and testing of a first Entity Bean using Eclipse, MyEcplise,
Jboss and xDoclet.
General
Author:
Sebastian
Hennebrüder
Date:
Revised
January,
7th 2005
Initial
Version
November,
1st 2004
Development
Tools
Eclipse
3.x
MyEclipse
plugin 3.8
(A
cheap and quite powerful Extension to Eclipse to develop Web
Applications and EJB (J2EE) Applications. I think that there is a
test version availalable at MyEclipse.)
Application
Server
Jboss
3.2.5
PDF-Version
des Tutorials:
http://www.laliluna.de/download/xdoclet_jboss_first_ejb.pdf
Introduction
If you do not
have experinces with development of enterprice java beans with
Eclipse, Jboss and xDoclet, this tutorial will help you to start.
Table
of Contents
Simple
Entity EJB – xDoclet, MyEclipse, Jboss and PostgreSql, MySql 1
General 1
Introduction 1
Create
an EJB Module Projects 1
Create
an EJB Class 2
Add
xDoclet functionality 4
Create
Datasource Mapping 6
Edit
source code 7
Run
xDoclet 8
Run
the jboss server 10
Test
the Bean 12
Congratulations
that it! 13
Create an
EJB Module Projects
Create a new
EJB Module Project. Right click on the package explorer or with
shortcut ?Strg + n?.

Create an
EJB Class
Within the
project create a new EJB.

Make
sure that the package ends with “.ejb” else xDoclet will
not work!
Now
you should see the generated source code.


Add xDoclet
functionality
Open
the project properties-
Choose
“MyEclipse-xDoclet” -
Right
click in the right upper window and choose ?Add Standard?. -
Selet Standard EJB and OK

Click on
“Standard EJB” in the right upper window.

Right
click on ejbdoclet and choose jboss from the list.

The following
settings must be add.
Jboss
Version, it is 3.2 for all 3.2.x-
the
destDir (where the the jboss.xml and jbosscmp-jdbc.xml will create) -
You
need a datasource, we will prepare this later. Take a name here like
“java:/tutorialDS”
Datasource
Mapping. Tells the Application Server what kind of field is used in
the DB for a jdbc-type. Find out the name yourself by looking into{jboss_home}serverdefaultconfstandardjbosscmp-jdbc.xml.
mysql
is mySQL for example. But we are using PostgreSQL!

Create
Datasource Mapping
Copy the driver to (you will find it on http://www.postgresql.org) to
jboss-3.2.4serverdefaultlib-
Create the database in Postgre.
-
Create the tables and two fields fid and fname with type text.
-
You can find examples configuration files for all supported DBs in<br>jboss-3.2.4docsexamplesjca
-
Copy the file postgres-ds.xml to<br>jboss-3.2.4serverdefaultdeploy
change the content of the file to:<br><br><datasources><br> <local-tx-datasource><br> <jndi-name>MyDS</jndi-name><br> <connection-url><br> jdbc:postgresql://localhost:5432/database-name<br> </connection-url><br> <driver-class>org.postgresql.Driver</driver-class><br> <user-name>username</user-name><br> <password>password</password><br> </local-tx-datasource><br></datasources><br>
Edit
source code
Find the
following position in the source code.
* @ejb.bean name = "SimpleBean"<br> * type = "CMP"<br> * cmp-version = "2.x"<br> * display-name = "SimpleBean"<br> * description = "SimpleBean EJB"<br> * view-type = "both"<br> * jndi-name = "ejb/SimpleBeanHome"<br> * local-jndi-name = "ejb/SimpleBeanLocalHome"<br> *<br> * @ejb:util<br> * generate="physical"<br> */<br>public abstract class SimpleBean implements EntityBean {<br><br>Add the following:<br><br> * @ejb.bean name = "SimpleBean"<br> * type = "CMP"<br> * cmp-version = "2.x"<br> * display-name = "SimpleBean"<br> * description = "SimpleBean EJB"<br> * view-type = "both"<br> * jndi-name = "ejb/SimpleBeanHome"<br> * local-jndi-name = "ejb/SimpleBeanLocalHome"<br> * primkey-field = "id"<br> * @ejb.persistence table-name = "tsimplebean"<br> * @jboss.persistence table-name = "tsimplebean"<br> * @ejb:util<br> * generate="physical"<br><br>Now we will add the Primary key field id and a second<br>field name<br><br>public abstract class SimpleBean implements EntityBean {<br><br> /** The EntityContext */<br> private EntityContext context;<br><br> /**<br> * @ejb.interface-method view-type = "both"<br> * @ejb.persistence column-name = "fid"<br> * @ejb.pk-field<br> *<br> * @return<br> */<br> public abstract String getId();<br><br> /**<br> * @ejb.interface-method view-type = "both"<br> *<br> * @param name<br> */<br> public abstract void setId(String id);<br><br> /**<br> * @ejb.interface-method view-type = "both"<br> * @ejb.persistence column-name = "fname"<br> *<br> * @return<br> */<br> public abstract String getName();<br><br><br><br><br> /**<br> * @ejb.interface-method view-type = "both"<br> *<br> * @param name<br> */<br> public abstract void setName(String name);<br>}
Run
xDoclet
We
have not completely finished the source code, but it is time for a
first generation with xdoclet.
Right click on the project and
choose run xdoclet.

Open
the console and look if everything is OK.

Your
package should have an interface package now, with the generated home
and local interfaces. You should have a jboss.xml and a
jbosscmp-jdbc.xml in src/META-INF.

Have a look
in the file SimpleBeanUtil. You will find some useful functions.
We
will use one to finish our bean. Change the ejbCreate method to the
following:
* @ejb.create-method<br> */<br> public String ejbCreate() throws CreateException<br> {<br> this.setId(SimpleUtil.generateGUID(this));<br> return null;<br> }<br><br>This will generate a random ID. That's it.
Run
the jboss server
Start
the jboss server and deploy the project.

Now
open http://localhost:8080/jmx-console
in your browser and select service JNDI-View, than select operation
?list?

You
can see your bean module now and it should also occur in the global
JNDI namespace.


Test
the Bean
Create
a Java project. Open the project properties and add the library j2ee
and the jar jbossall-client.

Include
your EJB project.

Create a new Class like the following and run it as java application.<br><br>package de.laliluna.tutorial.simpleBean;<br><br>import java.rmi.RemoteException;<br>import java.util.Properties;<br><br>import javax.ejb.CreateException;<br>import javax.ejb.EJBException;<br>import javax.naming.InitialContext;<br>import javax.naming.NamingException;<br>import javax.rmi.PortableRemoteObject;<br><br>import de.laliluna.tutorial.simpleBean.interfaces.Simple;<br>import de.laliluna.tutorial.simpleBean.interfaces.SimpleHome;<br><br>/**<br> * @author HS<br> *<br> * <br> */<br>public class SimpleBeanClient {<br><br> Properties properties;<br><br> public SimpleBeanClient() {<br> properties = new Properties();<br> properties.put("java.naming.factory.initial",<br> "org.jnp.interfaces.NamingContextFactory");<br> properties.put("java.naming.factory.url.pkgs",<br> "org.jboss.naming:org.jnp.interfaces");<br> properties.put("java.naming.provider.url", "jnp://localhost:1099");<br> properties.put("jnp.disableDiscovery", "true");<br> }<br><br> public static void main(String[] args) {<br> SimpleBeanClient beanClient = new SimpleBeanClient();<br> beanClient.createBean();<br> }<br><br> public void createBean() throws EJBException {<br> try {<br> // [laliluna] create a context to look up the beans in the JNDI<br> InitialContext context = new InitialContext(properties);<br> /*<br> * [laliluna] <br> * we have to look up the remote interaces as we are not in the same environment as the EJB.<br> * Therefore we will have to use the PortableRemote class to convert our object<br> */<br> Object object = context.lookup(SimpleHome.JNDI_NAME);<br> SimpleHome simpleHome = (SimpleHome) PortableRemoteObject.narrow(object,<br> SimpleHome.class);<br><br> Simple simple = simpleHome.create();<br> simple.setName("Gunter");<br> System.out.println(simple.getId());<br> System.out.println(simple.getName());<br><br> } catch (NamingException e) {<br> throw new EJBException(e);<br> } catch (RemoteException e) {<br> throw new EJBException(e);<br> } catch (CreateException e) {<br> throw new EJBException(e);<br> }<br><br> }<br>}<br>
Congratulations
that it!