Hi List,

For the last few days I've been trying to get a real simple CMP bean
deployed and working. All I want to do is get some information out of a
database but I can't seem to do it. I appologise before hand if this sort of
thing has alrady been answered but the search engine on geocrawler is down
and I've read heaps of manuals and the docs at jboss.org so many times now -
I probably wouldn't even find the mistake anymore. (Also if I've done
somethings completely stupid let me know)

okay here goes.

I have an entity bean called EmployeeBean the Employee.java file reads:
(note I've basically redone the CD collection example just for my own
understanding)

package org.workplacesupport.employee.interfaces;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface Employee extends EJBObject {

 public Integer getEmpID() throws RemoteException;

 public void setEmpID(Integer empid) throws RemoteException;

 public String getFirstName() throws RemoteException;

 public void setFirstName(String firstname) throws RemoteException;

 public String getLastName() throws RemoteException;

 public void setLastName(String lastname) throws RemoteException;
}

EmployeeHome.java

package org.workplacesupport.employee.interfaces;

import java.util.Collection;
import java.rmi.RemoteException;
import javax.ejb.*;

public interface EmployeeHome extends EJBHome {

 public Employee create(Integer empid)
  throws RemoteException, CreateException;

 public Employee findByPrimaryKey(Integer empid)
  throws RemoteException, FinderException;

  public Collection findAll()
     throws RemoteException, FinderException;
}

The EmployeeBean.java file reads:

package org.workplacesupport.employee.bean;

import javax.ejb.*;
import java.rmi.RemoteException;

public class EmployeeBean implements EntityBean {

  private EntityContext context;

  public Integer empid;
  public String firstname;
  public String lastname;

  public Integer ejbCreate (Integer _empid)
  {
    empid = _empid;
    return null;
  }

  public void ejbPostCreate(Integer empid) {}

  public String getFirstName() {return firstname;}

  public void setFirstName(String _firstname)
   { firstname = _firstname;}

  public String getLastName() {return lastname;}

  public void setLastName(String _lastname) { lastname = _lastname;}

  public Integer getEmpID() {return empid;}

  public void setEmpID(Integer _empid) { empid = _empid;}

  public void setEntityContext(EntityContext context) {
   this.context = context;
  }

  public void unsetEntityContext() {context = null;}

  public void ejbActivate() {}
  public void ejbPassivate() {}
  public void ejbRemove() {}
  public void ejbLoad() {}
  public void ejbStore() {}
}

I also have a Session bean called EmployeeCollection with remote interface:

EmployeeCollection.java

package org.workplacesupport.employee.interfaces;

import javax.ejb.EJBObject;
import javax.ejb.FinderException;
import java.rmi.RemoteException;


public interface EmployeeCollection
extends EJBObject
{

   public Employee[] findAll()
   throws RemoteException, FinderException;
}

EmployeeCollectionHome.java

package org.workplacesupport.employee.interfaces;

import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;

/**
 * This interface defines the home interface for the `CDCollection' EJB.
 */

public interface EmployeeCollectionHome
extends EJBHome
{
   EmployeeCollection create() throws RemoteException, CreateException;
}

EmployeeCollectionBean.java

package org.workplacesupport.employee.bean;

import org.workplacesupport.employee.interfaces.EmployeeHome;
import org.workplacesupport.employee.interfaces.Employee;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.util.Collection;

public class EmployeeCollectionBean implements SessionBean
{
   private Object getHome (String path, Class type)
 {
    try
  {
     InitialContext jndiContext = new InitialContext();
           Object ref = jndiContext.lookup(path);
           return PortableRemoteObject.narrow(ref,type);
  }
    catch (Exception e)
      {
         throw new EJBException(e);
      }
   }

   /**
    * Returns an array of all Employees in the collection
    */
   public Employee[] findAll() throws RemoteException, FinderException
 {
    EmployeeHome home = (EmployeeHome) getHome("employee/Employee",
EmployeeHome.class);
    Collection all = home.findAll();
    return (Employee[]) all.toArray(new Employee[0]);
 }

   public void ejbCreate() {}
   public void ejbRemove() {}
   public void ejbActivate() {}
   public void ejbPassivate() {}
   public void setSessionContext(SessionContext sc) {}

}


ejb-jar.xml reads

<?xml version="1.0"?>

<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise
JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd";>

<ejb-jar>
  <display-name>EmployeeRecords</display-name>
  <enterprise-beans>

    <entity>
      <description>Employee Test</description>
      <ejb-name>EmployeeBean</ejb-name>
      <home>org.workplacesupport.employee.interfaces.EmployeeHome</home>
      <remote>org.workplacesupport.employee.interfaces.Employee</remote>
      <ejb-class>org.workplacesupport.employee.bean.EmployeeBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Integer</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-field><field-name>empid</field-name></cmp-field>
      <cmp-field><field-name>firstname</field-name></cmp-field>
      <cmp-field><field-name>lastname</field-name></cmp-field>
      <primkey-field>empid</primkey-field>
    </entity>
    <session>
      <description>Models an Employee collection</description>
      <ejb-name>EmployeeCollectionBean</ejb-name>

<home>org.workplacesupport.employee.interfaces.EmployeeCollectionHome</home>

<remote>org.workplacesupport.employee.interfaces.EmployeeCollection</remote>

<ejb-class>org.workplacesupport.employee.bean.EmployeeCollectionBean</ejb-cl
ass>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
   <ejb-ref>
         <ejb-ref-name>ejb/Employee</ejb-ref-name>
         <ejb-ref-type>Entity</ejb-ref-type>
         <home>org.workplacesupport.employee.interfaces.EmployeeHome</home>
         <remote>org.workplacesupport.employee.interfaces.Employee</remote>
         <ejb-link>EmployeeBean</ejb-link>
   </ejb-ref>
    </session>

  </enterprise-beans>

  <assembly-descriptor>
    <container-transaction>
      <method>
        <ejb-name>EmployeeBean</ejb-name>
        <method-name>*</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
  </assembly-descriptor>
</ejb-jar>

jboss.xml reads

<?xml version="1.0"?>

<jboss>
    <secure>false</secure>
    <container-configurations />
    <resource-managers />
    <enterprise-beans>
            <session>
     <ejb-name>EmployeeCollectionBean</ejb-name>
     <jndi-name>employee/EmployeeCollection</jndi-name>
     <configuration-name>Standard Stateless SessionBean</configuration-name>
        </session>
        <entity>
     <ejb-name>EmployeeBean</ejb-name>
     <jndi-name>employee/Employee</jndi-name>
     <configuration-name>Standard CMP EntityBean</configuration-name>
        </entity>
    </enterprise-beans>
</jboss>

jaws.xml reads

<jaws>
 <enterprise-beans>

 <datasource>OracleDB</datasource>
 <type-mapping>Oracle8</type-mapping>

  <entity>
   <ejb-name>EmployeeBean</ejb-name>
   <table-name>EMPLOYEE</table-name>
   <create-table>false</create-table>
   <cmp-field>
    <field-name>empid</field-name>
    <column-name>EMPID</column-name>
   </cmp-field>
   <cmp-field>
    <field-name>firstname</field-name>
    <column-name>FIRSTNAME</column-name>
    <jdbc-type>VARCHAR</jdbc-type>
    <sql-type>VARCHAR(24)</sql-type>

   </cmp-field>
   <cmp-field>
    <field-name>lastname</field-name>
    <column-name>LASTNAME</column-name>
    <jdbc-type>VARCHAR</jdbc-type>
    <sql-type>VARCHAR(24)</sql-type>
   </cmp-field>
  </entity>
 </enterprise-beans>
</jaws>

and my ant build script reads

<?xml version="1.0" encoding="UTF-8" ?>

<project name="Employee Build Script" default="ejb-jar" basedir=".">

    <property name="build.employee.dir"
value="${basedir}/build-workplacesupport/employee"/>
    <property name="build.classes.dir"
value="${basedir}/build-workplacesupport/employee/classes"/>

    <target name="compile">
      <mkdir dir="${build.classes.dir}"/>
      <javac srcdir="${src.dir}"
           destdir="${build.classes.dir}"
           debug="on"
           deprecation="on"
           optimize="off" >
       <classpath path="${classpath}" />
       <include name="org/workplacesupport/employee/*.java" />
       <include name="org/workplacesupport/employee/bean/*.java" />
       <include name="org/workplacesupport/employee/interfaces/*.java" />
       <include name="org/workplacesupport/employee/client/*.java" />
      </javac>
    </target>


    <target name="ejb-jar" depends="compile">
       <delete dir="${build.employee.dir}/META-INF"/>
       <mkdir dir="${build.employee.dir}/META-INF"/>
       <copy
file="${src.dir}/org/workplacesupport/employee/resources/ejb-jar.xml"
todir="${build.employee.dir}/META-INF" />
       <copy
file="${src.dir}/org/workplacesupport/employee/resources/jboss.xml"
todir="${build.employee.dir}/META-INF" />
       <copy
file="${src.dir}/org/workplacesupport/employee/resources/jaws.xml"
todir="${build.employee.dir}/META-INF" />
       <jar jarfile="${build.employee.dir}/employee.jar">
            <fileset dir="${build.classes.dir}">
                <include
name="org/workplacesupport/employee/interfaces/Employee.class" />
                <include
name="org/workplacesupport/employee/interfaces/EmployeeHome.class" />
                <include
name="org/workplacesupport/employee/bean/EmployeeBean.class" />
  <include
name="org/workplacesupport/employee/interfaces/EmployeeCollection.class" />
                <include
name="org/workplacesupport/employee/interfaces/EmployeeCollectionHome.class"
/>
                <include
name="org/workplacesupport/employee/bean/EmployeeCollectionBean.class" />
            </fileset>
            <fileset dir="${build.employee.dir}">
                <include name="META-INF/ejb-jar.xml" />
                <include name="META-INF/jboss.xml" />
  <include name="META-INF/jaws.xml" />
            </fileset>
        </jar>
         <!-- JNDI Client used to connect to JBoss running on localhost -->
        <copy file="${basedir}/resources/jndi.properties"
tofile="${build.classes.dir}/jndi.properties" />
    </target>

    <target name="deploy-ejb-jar" depends="ejb-jar">
        <copy file="${build.employee.dir}/employee.jar"
todir="${jboss.dist}/deploy" />
    </target>

    <target name="employee-client" depends="compile">
        <java
classname="org.workplacesupport.employee.client.EmployeeClient" fork="yes">
            <classpath>
               <pathelement path="${classpath}"/>
               <pathelement location="${build.classes.dir}"/>
               <pathelement location="${src.resources}"/>
            </classpath>
        </java>
    </target>
</project>


and the jndi.properties reads

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

Now the oracle connection appears to be open as the start up gives no errors
and states that the driver is loaded and the connection pool starts

When the jar is deployed I get no errors (I can't seem to copy from the
windows command prompt) and then I run my client app:

EmployeeClient.java

package org.workplacesupport.employee.client;

import org.workplacesupport.employee.interfaces.Employee;
import org.workplacesupport.employee.interfaces.EmployeeHome;
import org.workplacesupport.employee.interfaces.EmployeeCollectionHome;
import org.workplacesupport.employee.interfaces.EmployeeCollection;
import javax.rmi.PortableRemoteObject;
import javax.naming.InitialContext;
import java.util.Hashtable;
import java.util.Properties;
import java.io.FileInputStream;

public class EmployeeClient
{

   public static void main(String[] args)
   {

      try
      {
         InitialContext jndiContext = new InitialContext();

         Object ref  = jndiContext.lookup("employee/EmployeeCollection");

         EmployeeCollectionHome home = (EmployeeCollectionHome)
            PortableRemoteObject.narrow (ref, EmployeeCollectionHome.class);

         EmployeeCollection employeeCollection = home.create();

         Employee[] employees = employeeCollection.findAll();

for (int i = 0; i < employees.length; i++) {
        System.out.println (employees[i].getEmpID() + "\t" +
employees[i].getFirstName() + "\t" + employees[i].getLastName());
   }
   }

      catch(Exception e)
      {
         System.out.println(e.toString());
      }
   }

}


then when I compile with ant and the client is run I get no information out
form the DB - there is at least one record in there which I entered
manually. I just can't seem to get this to work.

I'm using jdk1.3.1 JBoss2.2 with tomcat on win2000 and oracle 8i (8.1.7)

If anyone has any ideas I'd greatly appreciate it - I'm just too tired to
think of fresh ideas

Cheers

John Bruce




_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to