Simple Entity Ejb - Xdoclet, Myeclipse, Jboss and Postgresql, Mysql General
Simple Entity Ejb - Xdoclet, Myeclipse, Jboss and Postgresql, Mysql General
Simple Entity Ejb - Xdoclet, Myeclipse, Jboss and Postgresql, Mysql General
MySql
Creation and testing of a first Entity Bean using MyEcplise, Jboss and xDoclet.
General
Author:
Sebastian Hennebrüder
http://www.laliluna.de/tutorial.html – Tutorials for Struts, EJB, xdoclet and eclipse.
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/assets/tutorials/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
• 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
<datasources>
<local-tx-datasource>
<jndi-name>MyDS</jndi-name>
<connection-url>
jdbc:postgresql://localhost:5432/database-name
</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<user-name>username</user-name>
<password>password</password>
</local-tx-datasource>
</datasources>
Edit source code
Find the following position in the source code.
* @ejb.bean name = "SimpleBean"
* type = "CMP"
* cmp-version = "2.x"
* display-name = "SimpleBean"
* description = "SimpleBean EJB"
* view-type = "both"
* jndi-name = "ejb/SimpleBeanHome"
* local-jndi-name = "ejb/SimpleBeanLocalHome"
*
* @ejb:util
* generate="physical"
*/
public abstract class SimpleBean implements EntityBean {
/**
* @ejb.interface-method view-type = "both"
* @ejb.persistence column-name = "fid"
* @ejb.pk-field
*
* @return
*/
public abstract String getId();
/**
* @ejb.interface-method view-type = "both"
*
* @param name
*/
public abstract void setId(String id);
/**
* @ejb.interface-method view-type = "both"
* @ejb.persistence column-name = "fname"
*
* @return
*/
public abstract String getName();
/**
* @ejb.interface-method view-type = "both"
*
* @param name
*/
public abstract void setName(String name);
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
*/
public String ejbCreate() throws CreateException
{
this.setId(SimpleUtil.generateGUID(this));
return null;
}
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.
Create a new Class like the following and run it as java application.
package de.laliluna.tutorial.simpleBean;
import java.rmi.RemoteException;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import de.laliluna.tutorial.simpleBean.interfaces.Simple;
import de.laliluna.tutorial.simpleBean.interfaces.SimpleHome;
/**
* @author HS
*
*
*/
public class SimpleBeanClient {
Properties properties;
public SimpleBeanClient() {
properties = new Properties();
properties.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
properties.put("java.naming.provider.url", "jnp://localhost:1099");
properties.put("jnp.disableDiscovery", "true");
}
} catch (NamingException e) {
throw new EJBException(e);
} catch (RemoteException e) {
throw new EJBException(e);
} catch (CreateException e) {
throw new EJBException(e);
}
}
}