1 Reply Latest reply on Dec 27, 2006 6:59 AM by alexg79

    EJB 3.0 AND JBOSS 4.0.4 G.A

    priyavijayan1

      Hi,

      I am having a problem with EJB 3.0 . I am using
      EJB-3.0_RC9-FD
      jboss-4.0.4.GA
      jdk 1.5
      ant 1.6.5
      I am using the hypersonic database of jboss and I want to create two tables in the database Department and Emplyee.


      The code for client.java

      package com.abcd.EJB3.application;

      import com.infosys.EJB3.application.FacadeRemote;
      import javax.naming.InitialContext;
      import java.sql.Timestamp;

      public class client
      {
      public static void main(String[] args)
      {
      try
      {
      InitialContext ctx = new InitialContext();
      FacadeRemote lFacadeRemote = (FacadeRemote) ctx.lookup(FacadeRemote.class.getName());

      lFacadeRemote.createDepartment("Finance");
      lFacadeRemote.createDepartment("Administration");
      lFacadeRemote.createDepartment("Sales");

      lFacadeRemote.assignEmployeeToDepartment(new Long(1),"Brijesh",31,"M");
      lFacadeRemote.assignEmployeeToDepartment(new Long(1),"xxxx",31,"M");
      lFacadeRemote.assignEmployeeToDepartment(new Long(2),"yyyy",31,"M");

      }
      catch(Exception ex)
      {
      System.out.println(ex);
      }
      }
      }



      The code for department.java

      package com.abcd.EJB3.application;

      import java.util.ArrayList;
      import java.util.Collection;

      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;

      @Entity
      @Table(name = "DEPARTMENT")
      public class Department implements java.io.Serializable
      {
      private Long id;
      private String lsName;
      private Collection lEmployees;

      //@Id(generate = GeneratorType.AUTO)
      @Id @GeneratedValue(strategy=GenerationType.AUTO)

      public Long getId()
      {
      return id;
      }

      public void setId(Long id)
      {
      this.id = id;
      }

      @Column(name = "NAME")
      public String getName()
      {
      return lsName;
      }

      public void setName(String asName)
      {
      this.lsName = asName;
      }

      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="department")
      public Collection getEmployees()
      {
      return lEmployees;
      }

      public void setEmployees(Collection aEmployees)
      {
      this.lEmployees = aEmployees;
      }

      public void addEmployee(String asName,int aiAge, String asSex)
      {
      if(lEmployees== null)
      lEmployees = new ArrayList();

      Employee lEmployee = new Employee();
      lEmployee.setName(asName);
      lEmployee.setAge(aiAge);
      lEmployee.setSex(asSex);
      lEmployee.setDepartment(this);
      lEmployees.add(lEmployee);

      setEmployees(lEmployees);
      }
      }


      the code for Employee.java

      package com.abcd.EJB3.application;

      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.Column;
      import javax.persistence.Table;
      import javax.persistence.FetchType;
      import javax.persistence.ManyToMany;
      import javax.persistence.CascadeType;
      import javax.persistence.PostPersist;


      import java.util.Set;

      //@Entity
      public class Employee implements java.io.Serializable
      {
      private Long id;
      private String lsName;
      private int liAge;
      private String lsSex;
      private Department department;

      public Employee()
      {
      }

      //@Id(generate = GeneratorType.IDENTITY)
      // @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
      public Long getId()
      {
      return id;
      }

      public void setId(Long id)
      {
      this.id = id;
      }

      // @Column(name = "NAME")
      public String getName()
      {
      return lsName;
      }

      public void setName(String asName)
      {
      this.lsName = asName;
      }

      // @Column(name = "AGE")
      public int getAge()
      {
      return liAge;
      }

      public void setAge(int liAge)
      {
      this.liAge = liAge;
      }

      // @Column(name = "SEX")
      public String getSex()
      {
      return lsSex;
      }

      public void setSex(String lsSex)
      {
      this.lsSex = lsSex;
      }

      // @ManyToOne
      // @JoinColumn(name="DEPARTMENT_ID")
      public Department getDepartment()
      {
      return department;
      }

      public void setDepartment(Department aDepartment)
      {
      this.department = aDepartment;
      }

      // @PostPersist
      public void samplePostPersist()
      {
      System.out.println("Added Employee having name = "+ this.getName() +" and Age = "+ this.getAge());
      }
      }


      code for FacadeBean

      package com.abcd.EJB3.application;

      import javax.ejb.Stateless;
      import javax.ejb.Remote;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;

      import com.infosys.EJB3.application.Department;
      import com.infosys.EJB3.application.Employee;

      import java.util.Set;

      @Stateless
      public class FacadeBean implements FacadeRemote
      {
      @PersistenceContext
      private EntityManager manager;

      public void createDepartment(String deptName)
      {
      Department department = new Department();

      department.setName(deptName);

      manager.persist(department);

      }

      public void assignEmployeeToDepartment(Long id, String name, int age, String sex)
      {
      Department department = searchDepartment(id);

      department.addEmployee(name, age, sex);

      manager.persist(department);
      }

      public Department searchDepartment(Long id)
      {
      return manager.find(Department.class,id);
      }

      }


      code for FacadeRemote


      package com.abcd.EJB3.application;

      import javax.ejb.Remote;

      @Remote
      public interface FacadeRemote
      {
      public void createDepartment(String deptName);
      public Department searchDepartment(Long id);
      public void assignEmployeeToDepartment(Long id, String name, int age, String sex);
      }



      now I have compiled the code and deployed it. But when I try to run the code I get the following error:


      NameNotFoundException ----- FacadeRemote not bound.

      I think its something related to JNDI binding.. but here since I am using EJB 3.0, i dont have to use ejb-jar.xml, but then how does this happens?? what should I do... Please help....

        • 1. Re: EJB 3.0 AND JBOSS 4.0.4 G.A
          alexg79

          Please, in future posts wrap your code using the "code" button on the posting page (where you write your post). That way it'll preserve the indentation and use a monospace font so it'll be much easier to read.
          Regarding your problem, the JNDI path you're using is wrong, assuming you use JBoss of course. I don't know where you got that code, but the correct JNDI path would be APPNAME/FacadeBean/remote. Replace APPNAME with the name of your deployed application. It's best to verify from the JMX console that the resulting JNDI path actually exists on your server after deployment.

          One more tip:
          MessageFormat.format("{0}/{1}Bean/remote", APPNAME, beanName);
          This is what I use in my own application.