com.ejb.entity package
1). NamedQueryA remote interface.
package com.ejb.entity;
import java.rmi.RemoteException;import java.util.Collection;
/** * NamedQuery entity bean interface * @author KCN Reddy * @version 1.0 * @since 1.0 */public interface NamedQueryA extends javax.ejb.EJBLocalObject { public void setBName(String bname); public String getBName(); public void setBColor(String bcolor); public String getBColor();
public void setBQuantity(int bquantity); public int getBQuantity(); public long calicul(long a ,long b) ; }
2). NamedQueryA home
package com.ejb.entity;
import javax.ejb.CreateException;import javax.ejb.FinderException;
/** * NamedQuery home interface * @author KCN Reddy * @version 1.0 * @since 1.0 */public interface NamedQueryAHome extends javax.ejb.EJBLocalHome {
//public NamedQuery create(NamedQueryData nqData) throws CreateException; public NamedQueryA create(String bname,String bcol,int quantity) throws CreateException;
// public NamedQueryA findByPrimaryKey(NamedQueryAPK nqPK) throws FinderException; public NamedQueryA findByPrimaryKey(String nqPK) throws FinderException;}
3). Bean class
package com.ejb.entity;
import java.rmi.RemoteException;import java.util.Collection;import javax.ejb.CreateException;import javax.ejb.EntityBean;import javax.ejb.EntityContext;import javax.ejb.FinderException;import javax.ejb.RemoveException;
/** * NamedQuery information entity bean class * @author KCN Reddy * @version 1.0 * @since 1.0 */public abstract class NamedQueryABean implements EntityBean {
EntityContext entityContext; String bname; String bcolor; int bquantity; public java.lang.String ejbCreate(String bname,String bcolor,int quantity) throws CreateException{ this.bname = bname; this.setBColor(bcolor); this.setBName(bname); this.setBQuantity(quantity); return bname; } public void ejbPostCreate(String bname,String bcolor,int quantity) throws CreateException { // since there are no relations, this is empty. /** @todo Complete this method*/ }
public void ejbRemove() throws RemoveException {/** @todo Complete this method*/}
public abstract void setBName(String bname); public abstract String getBName(); public abstract void setBColor(String bcolor); public abstract String getBColor(); public abstract void setBQuantity(int bquantity); public abstract int getBQuantity();
public void ejbLoad() {/** @todo Complete this method*/}
public void ejbStore() {/** @todo Complete this method*/}
public void ejbActivate() {/** @todo Complete this method*/}
public void ejbPassivate() {/** @todo Complete this method*/}
public void unsetEntityContext() { this.entityContext = null; }
public void setEntityContext(EntityContext entityContext) { this.entityContext = entityContext; } public long calicul(long a,long b) { System.out.println("------------caliculating---------hai------------"); return a; } }
4). NamedQueryAPK class
package com.ejb.entity;
import java.io.Serializable;
public class NamedQueryAPK implements Serializable {
public String bName ;
public NamedQueryAPK() {}
public NamedQueryAPK(String bName) {
this.bName = bName;
}
public boolean equals(Object obj) {
if (obj != null) {
if (this.getClass().equals(obj.getClass())) {
NamedQueryAPK that = (NamedQueryAPK) obj;
return this.bName.equals(that.bName);
}
}
return false;
}
public int hashCode() {
return (bName).hashCode();
}
}
com.ejb. session package.
1). Remote interface.
package com.ejb.session;
import javax.ejb.*;import java.rmi.RemoteException;
public interface FirstSession extends EJBObject{ public long caliculate(long a ,long b) throws RemoteException; public String insertItem(String bname, String bcolor,int bquantity) throws RemoteException; }
2).Home interface.
package com.ejb.session;
import javax.ejb.*;import java.rmi.RemoteException;
public interface FirstSessionHome extends EJBHome { public FirstSession create()throws RemoteException, CreateException;
}
3).Bean class
package com.ejb.session;
import java.rmi.RemoteException;import java.util.Properties;
import javax.ejb.*;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.rmi.PortableRemoteObject;
import com.ejb.entity.NamedQueryA;import com.ejb.entity.NamedQueryAHome;
public class FirstSessionBean implements SessionBean { private SessionContext sessionContext; Properties p = new Properties();
public String insertItem(String bname,String bcolor,int bquantity) throws RemoteException{ System.out.println("inside insert Item method of session bean"); long value =0; try { InitialContext ectx = new InitialContext(p); NamedQueryAHome nHome = (NamedQueryAHome)(ectx.lookup("java:comp/env/ejb/NamedQueryA")); NamedQueryA nQuery = nHome.create(bname,bcolor,bquantity); //System.out.println(" After getting the named query object "+nQuery.getBName()); //value = nQuery.calicul(12,23); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (CreateException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""+value; } public void ejbCreate(){ } public void ejbRemove(){ } public void ejbActivate(){ } public void ejbPassivate(){ } public void setSessionContext(SessionContext sessionContext){ this.sessionContext = sessionContext; } public long caliculate(long a,long b) throws RemoteException{ System.out.println("------------caliculating---------hai------------"); return a; } }
META-INF.
1) ejb-jar.xml
EJB1
session bean
FirstSession
FirstSession
com.ejb.session.FirstSessionHome
com.ejb.session.FirstSession
com.ejb.session.FirstSessionBean
Stateless
Container
ejb/NamedQueryA
Entity
com.ejb.entity.NamedQueryAHome
com.ejb.entity.NamedQueryA
NamedQueryA
This is description
NamedQueryA
NamedQueryA
com.ejb.entity.NamedQueryAHome
com.ejb.entity.NamedQueryA
com.ejb.entity.NamedQueryABean
Container
java.lang.String
false
2.x
NamedQueryA
BName
BColor
BQuantity
BName
This role represents everyone who is allowed full access
to the cabin bean.
everyone
everyone
NamedQueryA
*
2).jbosscmp-jdbc.xml
java:/JMetroJDBCDataSource
MS SQLSERVER2000
false
false
NamedQueryA
java:/JMetroJDBCDataSource
MS SQLSERVER2000
MYBEER
BName
BName
BColor
BColor
BQuantity
BQuantity
Client FILE:
1).
package com.ejb.session;
import java.util.Properties;
import javax.naming.Context;import javax.naming.InitialContext;import javax.rmi.PortableRemoteObject;
public class Client { public static void main( String[] args){ try{ 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"); InitialContext ctx = new InitialContext(p); Object obj = ctx.lookup("FirstSession"); FirstSessionHome ejbHome = ( FirstSessionHome ) PortableRemoteObject.narrow(obj, FirstSessionHome .class); FirstSession sessionObj = ejbHome.create(); System.out.println("---------------"); String aaa = sessionObj.insertItem("Bhaskar","Yellow",2); System.out.println("----------k-------------"+aaa); }catch(Exception e){ e.printStackTrace(); } } }
jar files i added are:
1). jboss 4.0.5ga/server/default/lib : jboss-j2ee.jar, javax.servlet.jar,javax.servlet.jsp.jar.
2).jboss 4.0.5ga/client/lib: jbossall-client.jar.
* I used eclipse europa to crate a project. And i create a table in the database MYBEER.
QUERY IS
create table MYBEER( BName char(30) primary key, BColor char(30), BQuantity int) .