Wednesday, September 24, 2008

Studies --> java --> ejb -- > entity --> simple Example

I created a simple entity bean which inserts a row in a table .

1). Remote interface.

package com.titan.cabin;
import java.rmi.RemoteException;
public interface Cabin extends javax.ejb.EJBObject {
public String getName() throws RemoteException;
public void setName(String str) throws RemoteException;
public int getDeckLevel() throws RemoteException;
public void setDeckLevel(int level) throws RemoteException;
public int getShip() throws RemoteException;
public void setShip(int sp) throws RemoteException;
public int getBedCount() throws RemoteException;
public void setBedCount(int bc) throws RemoteException;
}

2). Home interface
package com.titan.cabin;
import java.rmi.RemoteException;import javax.ejb.CreateException;import javax.ejb.FinderException;
public interface CabinHome extends javax.ejb.EJBHome {
public Cabin create(int i,String n,int a, int b,int id) throws CreateException, RemoteException;
public Cabin findByPrimaryKey(CabinPK pk) throws FinderException, RemoteException;
}

3). primary key class

package com.titan.cabin;
public class CabinPK implements java.io.Serializable {
public int id;
String cname = null;
public CabinPK(){}
public CabinPK(int id) { this.id = id; }
public boolean equals(Object obj) {
if (obj != null) {
if (this.getClass().equals(obj.getClass())) {
CabinPK that = (CabinPK) obj;
return this.id == that.id;
}
}
return false;
}
public int hashCode() {
return (""+id).hashCode();
}
}

4). Bean class
package com.titan.cabin;
import javax.ejb.EntityContext;
public class CabinBean implements javax.ejb.EntityBean {
public int id; public String name;
public int deckLevel;
public int ship;
public int bedCount;
public CabinPK ejbCreate(int i,String a, int b, int c,int d) {
this.id = i;
this.setName(a);
this.setDeckLevel(b);
this.setShip(c);
this.setBedCount(d);
//this.id = id;
//return null;
return new CabinPK(id);
}
public void ejbPostCreate(int i,String a, int b, int c,int d) { // Do nothing. Required. } public String getName() { return name; }
public void setName(String str) { name = str; }
public int getShip() { return ship; }
public void setShip(int sp) { ship = sp; }
public int getBedCount() { return bedCount; }
public void setBedCount(int bc) { bedCount = bc; }
public int getDeckLevel() { return deckLevel; }
public void setDeckLevel(int level ) { deckLevel = level; }
public void setEntityContext(EntityContext ctx) { // Not implemented. }
public void unsetEntityContext() { // Not implemented. }
public void ejbActivate() { // Not implemented. }
public void ejbPassivate() { // Not implemented. }
public void ejbLoad() { // Not implemented. }
public void ejbStore() { // Not implemented. }
public void ejbRemove() { // Not implemented. }
}

5). ejb-jar.xml







This Cabin enterprise bean entity represents a cabin on
a cruise ship.

CabinBean
com.titan.cabin.CabinHome
com.titan.cabin.Cabin
com.titan.cabin.CabinBean
Container
com.titan.cabin.CabinPK
False
id
name
deckLevel
ship
bedCount





This role represents everyone who is allowed full access
to the cabin bean.

everyone


everyone

CabinBean
*




CabinBean
*

Required




6). jbosscmp-jdbc.xml







java:/JMetroJDBCDataSource
MS SQLSERVER2000
false
false



CabinBean
java:/JMetroJDBCDataSource
MS SQLSERVER2000
CABIN

id
ID


name
NAME


deckLevel
DECK_LEVEL


ship
SHIP_ID


bedCount
BED_COUNT






7). Client.java

package com.titan.cabin;
import com.titan.cabin.CabinHome;import com.titan.cabin.Cabin;
import com.titan.cabin.CabinPK;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.util.Properties;
public class Client_1 {
public static void main(String [] args) {
try {
Context jndiContext = getInitialContext();
Object ref = jndiContext.lookup("CabinBean");
CabinHome home = (CabinHome) // EJB 1.0:Use Java cast instead of narrow( ) PortableRemoteObject.narrow(ref,CabinHome.class);
Cabin cabin_1 = home.create(1,"MyName",2,2,3);
// cabin_1.setName("Master Suite");
// cabin_1.setDeckLevel(1);
// cabin_1.setShip(1);
// cabin_1.setBedCount(3);
// // CabinPK pk = new CabinPK();
// pk.id = 1;
// // Cabin cabin_2 = home.findByPrimaryKey(pk);
// System.out.println(cabin_2.getName());
// System.out.println(cabin_2.getDeckLevel());
// System.out.println(cabin_2.getShip());
// System.out.println(cabin_2.getBedCount());
} catch (java.rmi.RemoteException re)
{re.printStackTrace();
} catch (javax.naming.NamingException ne){ne.printStackTrace();
} catch (javax.ejb.CreateException ce){ce.printStackTrace();
} //catch (javax.ejb.FinderException fe){fe.printStackTrace();
}
}

public static Context getInitialContext() throws javax.naming.NamingException {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
// ... Specify the JNDI properties specific to the vendor. return new javax.naming.InitialContext(p);
}
}


8). You need to create a table
create table CABIN ( ID int primary key, SHIP_ID int, BED_COUNT int, NAME char(30), DECK_LEVEL int)

Note: you need to create this table in the data base i.e refred by application server.
for example in jboss we will refer the database in jboss-ds.xml. So see the datasource name in the jboss-ds.xml and create the table in that.

keep the ejb-jar.xml and jbosscmp-jdbc.xml in web-inf folder and deploy the jar in server.

--------------------------
1) Client to fet the record from the table
package com.titan.cabin;
import com.titan.cabin.CabinHome;import com.titan.cabin.Cabin;import com.titan.cabin.CabinPK;
import javax.naming.InitialContext;import javax.naming.Context;import javax.naming.NamingException;import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;import java.util.Properties;
public class Client_1 {
public static void main(String [] args) {
try {
Context jndiContext = getInitialContext();
Object ref = jndiContext.lookup("CabinBean");
CabinHome home = (CabinHome) // EJB 1.0:Use Java cast instead of narrow( ) PortableRemoteObject.narrow(ref,CabinHome.class);
// Cabin cabin_1 = home.create(1,"MyName",2,2,3);
// cabin_1.setName("Master Suite");
// cabin_1.setDeckLevel(1);
// cabin_1.setShip(1);
// cabin_1.setBedCount(3);
CabinPK pk = new CabinPK();
pk.id = 1;
Cabin cabin_2 = home.findByPrimaryKey(pk);
System.out.println(cabin_2.getName());
System.out.println(cabin_2.getDeckLevel());
System.out.println(cabin_2.getShip());
System.out.println(cabin_2.getBedCount());
} catch (java.rmi.RemoteException re){re.printStackTrace();} catch (javax.naming.NamingException ne){ne.printStackTrace();} //catch (javax.ejb.CreateException ce){ce.printStackTrace();} catch (javax.ejb.FinderException fe){fe.printStackTrace();} }

No comments: