About Java and JRuby Development
JEE, Spring, Guice
Hibernate, Java Persistence (JPA)
and various Web Frameworks

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?.

new ejb project

Create an
EJB Class

Within the
project create a new EJB.









Now
you should see the generated source code.










Add xDoclet
functionality







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.










Create
Datasource Mapping


change the content of the file to:<br><br>&lt;datasources&gt;<br>  &lt;local-tx-datasource&gt;<br>    &lt;jndi-name&gt;MyDS&lt;/jndi-name&gt;<br>    &lt;connection-url&gt;<br>    jdbc:postgresql://localhost:5432/database-name<br>    &lt;/connection-url&gt;<br>    &lt;driver-class&gt;org.postgresql.Driver&lt;/driver-class&gt;<br>    &lt;user-name&gt;username&lt;/user-name&gt;<br>    &lt;password&gt;password&lt;/password&gt;<br>  &lt;/local-tx-datasource&gt;<br>&lt;/datasources&gt;<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!