0 Replies Latest reply on Sep 2, 2006 11:07 PM by Dasari Prasad

    EJB3.0-Relations one-to-many

    Dasari Prasad Newbie

      Dear colleague,

      I had developed one one to many relation(bi-directional) as a part of learning having two entity beans and one Stateful session facade for them. i could able to create entity beans and add a mny bean to the collection of one-side bean.but later when i search for individual beans ,it is showing errors and returning a null.In the server log file ,it is getting the information but not able to return to the client.The code is

      one side Entity

      package cmp3rels;

      import javax.persistence.*;
      import javax.ejb.*;
      import java.util.Set;
      import java.util.HashSet;

      @Entity()
      @Table(name="AREA")
      @NamedQuery(name="plain.areas ", query="select a from Area a ")

      public class Area implements java.io.Serializable
      {
      Integer id;
      String areaName;
      int population;
      Set bazars = new HashSet();


      @Id
      public Integer getId()
      {
      return id;
      }

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

      @Column(name="AREA_NAME" ,nullable=false)
      public String getAreaName()
      {
      return areaName;
      }

      public void setAreaName(String newAreaName)
      {
      areaName = newAreaName;
      }

      @Column
      public int getPopulation()
      {
      return population;
      }

      public void setPopulation(int newPopulation)
      {
      population = newPopulation;
      }

      @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER, mappedBy="area")
      @JoinColumn(name = "AREA_ID")
      public Set getBazars()
      {
      return bazars;
      }

      public void setBazars(Set newBazars)
      {
      bazars = newBazars;
      }

      public String toString()
      {
      StringBuffer sb = new StringBuffer();
      sb.append("Id:"+id);
      sb.append("\nArea-Name:"+areaName);
      sb.append("\nPopulation:"+population);
      sb.append("\nbazars:"+bazars+"\n");
      return sb.toString();
      }

      }
      ............................................................................................................
      many-side entity
      package cmp3rels;

      import javax.persistence.*;
      import javax.ejb.*;

      @Entity()
      @Table(name="BAZAR")
      @NamedQuery(name="plain.bazars", query="select b from Bazar b ")


      public class Bazar implements java.io.Serializable
      {
      Integer id;
      String bazarName;
      double turnover;
      Area area;

      @Id
      public Integer getId()
      {
      return id;
      }

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

      @Column(name="BAZAR_NAME" ,nullable=false)
      public String getBazarName()
      {
      return bazarName;
      }

      public void setBazarName(String newBazarName)
      {
      bazarName = newBazarName;
      }

      @Column
      public double getTurnover()
      {
      return turnover;
      }

      public void setTurnover(double newTurnover)
      {
      turnover = newTurnover;
      }


      @ManyToOne(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
      @JoinColumn(name="AREA_ID" )
      public Area getArea()
      {
      return area;
      }

      public void setArea(Area newArea)
      {
      area = newArea;
      }

      public String toString()
      {
      StringBuffer sb = new StringBuffer();
      sb.append("Id: "+id);
      sb.append("\nArea-Name: "+bazarName);
      sb.append("\nTurnover: "+turnover);
      sb.append("\nArea: "+area+"\n");
      return sb.toString();
      }

      }
      ........................................................................................................
      stateful facade is
      interface

      package cmp3rels;

      import java.util.*;
      import javax.ejb.Remote;

      @Remote
      public interface AreaSess
      {

      public boolean createArea(int id, String areaName, int population);

      public boolean addBazarToArea(int areaCode, int bazarCode);

      public boolean assignBazars(int areaCode , Set bazars);

      public String getAreaInfo(Integer areaCode);

      public boolean createBazar(int id, String bazarName, double turnover);

      public Bazar getCodeBazar(Integer bazarCode);

      public Area getCodeArea(Integer areaCode);

      public Area getBazarArea(Bazar bazar);

      public Area newArea(int id, String areaName, int population);

      public Bazar newBazar(int id, String bazarName, double tover);



      }

      package cmp3rels;

      import javax.ejb.Stateful;
      import javax.persistence.PersistenceContext;
      import javax.persistence.PersistenceContextType;
      import javax.persistence.EntityManager;
      import javax.persistence.EntityManagerFactory;
      import javax.persistence.EntityTransaction;
      import javax.persistence.Query;
      import org.jboss.ejb3.entity.*;
      import java.util.Set;
      import java.util.HashSet;
      import java.util.List;

      @Stateful
      public class AreaSessBean implements AreaSess,java.io.Serializable
      {
      @PersistenceContext(unitName="mydb", type=PersistenceContextType.EXTENDED)
      private EntityManager em;

      EntityTransaction trans;

      private Area area;

      private Bazar bazar;



      public boolean createArea(int id, String areaName, int population)
      {
      boolean ret = false;

      try
      {
      Area area = new Area();
      area.setId(new Integer(id));
      area.setAreaName(areaName);
      area.setPopulation(population);
      em.persist(area);
      ret = true;

      }
      catch(Exception e)
      {

      e.printStackTrace(System.out);
      }
      return ret;
      }

      public boolean addBazarToArea(int areaCode, int bazarCode)
      {
      boolean ret = false;
      Area area = null;
      Bazar bazar = null;
      Set bazars = null ;


      try
      {
      area =(Area)em.find(Area.class,new Integer(areaCode));
      System.out.println(area);
      bazar =(Bazar)em.find(Bazar.class,new Integer(bazarCode));

      if(area != null )
      {
      bazar.setArea(area);
      bazars = area.getBazars();
      bazars.add(bazar);
      em.merge(area);
      em.merge(bazar);

      ret = true;
      }
      }
      catch(Exception e)
      {
      e.printStackTrace(System.out);
      }
      return ret;
      }

      public Area getCodeArea(Integer areaCode)
      {
      Area ret = null;
      try
      {
      System.out.println("param is:"+areaCode);
      ret = (Area)em.getReference(Area.class, areaCode);
      em.flush();
      }
      catch(Exception e)
      {

      StackTraceElement[] stck = e.getStackTrace();
      System.out.println("1....:"+stck.length);
      for(StackTraceElement elem : stck)
      {
      System.out.println(elem);
      }
      }

      System.out.println("area is:"+ret);
      return ret;
      }

      public Bazar getCodeBazar(Integer bazarCode)
      {
      Bazar ret = null;
      try
      {
      System.out.println("param is :"+bazarCode);
      ret = (Bazar)em.getReference(Bazar.class,bazarCode);
      }
      catch(Exception e)
      {


      StackTraceElement[] stck = e.getStackTrace();
      System.out.println("1....:"+stck.length);

      for(StackTraceElement elem : stck)
      {
      System.out.println(elem);
      }
      }
      System.out.println("1....:"+ret);
      return ret;
      }

      public boolean assignBazars(int areaCode , Set bazs)
      {
      boolean ret = false;
      Area area = null;
      Set bazars = null ;
      try
      {
      area = (Area)em.find(cmp3rels.Area.class,new Integer(areaCode));
      if(area != null )
      {
      bazars = area.getBazars();
      bazars.clear();
      bazars.addAll(bazs);

      for(Bazar bz : bazs)
      {
      bz.setArea(area);
      }

      ret = true;
      }
      }
      catch(Exception e)
      {

      e.printStackTrace(System.out);
      }
      return ret;
      }

      /* public String getAreaInfo(Integer areaCode)
      {
      String ret = "Not Available";
      Area area = null;
      String qryStr = "select Object(ar) from Area ar where ar.id=:d";
      System.out.println("...."+areaCode);

      try
      {
      Query qry = em.createQuery(qryStr);
      qry.setParameter("d",areaCode);
      System.out.println(qry);
      List li = qry.getResultList();
      if(li.size()>0 )
      {
      area = (Area)li.get(0);
      ret = area.toString();
      }

      }
      catch(Exception e)
      {

      StackTraceElement[] stck = e.getStackTrace();
      System.out.println("1....:"+stck.length);
      for(StackTraceElement elem : stck)
      {
      System.out.println(elem);
      }

      }
      return ret;
      }*/


      public String getAreaInfo(Integer areaCode)
      {
      String ret = "Not Available";
      Area area = null;

      try
      {
      area =(Area)em.getReference(Area.class,areaCode);
      if(area != null )
      {
      ret = area.toString();
      }

      }
      catch(Exception e)
      {

      StackTraceElement[] stck = e.getStackTrace();
      System.out.println("1....:"+stck.length);
      for(StackTraceElement elem : stck)
      {
      System.out.println(elem);
      }

      }
      return ret;
      }


      public boolean createBazar(int id, String bazarName, double turnover)
      {
      boolean ret = false;

      try
      {
      Bazar bazar = new Bazar();
      bazar.setId(new Integer(id));
      bazar.setBazarName(bazarName);
      bazar.setTurnover(turnover);
      em.persist(bazar);

      ret = true;
      }
      catch(Exception e)
      {

      e.printStackTrace(System.out);
      }
      return ret;
      }

      public Area getBazarArea(Bazar bazar1)
      {
      System.out.println("...."+em.isOpen());
      String qryStr = " select ar from Area ar ,in (ar.bazars) brs " +
      " where :b1 member of brs ";
      Area area = null;
      Bazar bazar = null;
      try
      {
      Query qry = em.createQuery(qryStr);
      qry.setParameter("b1",bazar1);
      List li = qry.getResultList();
      area = (Area)li.get(0);

      }
      catch(Exception e)
      {

      e.printStackTrace(System.out);
      }
      return area;
      }

      public Area newArea(int id, String areaName, int population)
      {
      Area ret = null;

      try
      {
      Area area = new Area();
      area.setId(new Integer(id));
      area.setAreaName(areaName);
      area.setPopulation(population);
      em.persist(area);
      ret = area;
      }
      catch(Exception e)
      {

      e.printStackTrace(System.out);
      }
      return ret;
      }


      public Bazar newBazar(int id, String bazarName, double tover)
      {
      Bazar ret = null;

      try
      {
      Bazar bazar = new Bazar();
      bazar.setId(new Integer(id));
      bazar.setBazarName(bazarName);
      bazar.setTurnover(tover);
      em.persist(bazar);
      ret = bazar;
      }
      catch(Exception e)
      {

      e.printStackTrace(System.out);
      }
      return ret;
      }


      }

      ...................................................................................................

      working client class is
      package cmp3rels;

      import javax.ejb.*;
      import javax.naming.*;
      import javax.rmi.*;
      import java.util.*;
      import org.apache.log4j.*;

      public class NewRel3Client
      {
      static Logger logger;

      public static InitialContext getInitialContext()
      {
      Properties props=new Properties();
      InitialContext jndiCtx=null;
      try
      {
      props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
      props.put(Context.PROVIDER_URL,"jnp://localhost");
      props.put(Context.STATE_FACTORIES,"org.jboss.naming:org.jnp.interfaces");
      jndiCtx=new InitialContext(props);
      }
      catch(Exception ex)
      {}
      return jndiCtx;
      }

      public static void main(String args[])throws java.io.IOException
      {
      int[] areaIds = {1000,1005,1010};
      String[] areas = {"Guindy","Tambaram","Mudichur"};
      int[] pops = {490000,260000,95000};

      int[] bazarIds = {100,105,110};
      String bazars[] = {"Super-Special","Variety-Mart","Needs-Market"};
      double tovers[] = {239879.30,199234.30,61439.30};

      boolean created = false;
      logger = Logger.getRootLogger();
      logger.addAppender(new FileAppender(new HTMLLayout(),
      "mylog1.log",true));
      System.out.println("\nBegin New-EJB3-Rels-Client...\n");
      try
      {

      InitialContext ctx=getInitialContext();

      AreaSess sess =
      (AreaSess)ctx.lookup("AreaSessBean/remote");

      System.out.println("\nGot jndi named object..\n");
      for(int i=0;i<3;i++)
      {
      created = sess.createArea(areaIds,areas,pops);

      System.out.println("\nStatement Area Created is...:"+created);

      created = sess.createBazar(bazarIds
      ,bazars,tovers);

      System.out.println("\nStatement Bazar Created is...:"+created);
      }
      Area area = null;
      Bazar bazar = null;

      for(int i=0;i<15;i+=5)
      {
      created = sess.addBazarToArea(1000+i,100+i);
      System.out.println("bazar is added "+created);
      }


      }
      catch(Exception e)
      {
      e.printStackTrace();
      }

      }

      }

      ............................................................................................................

      not working class is

      package cmp3rels;

      import javax.ejb.*;
      import javax.naming.*;
      import javax.rmi.*;
      import java.util.*;
      import org.apache.log4j.*;

      public class Rel3Client
      {
      static Logger logger;

      public static InitialContext getInitialContext()
      {
      Properties props=new Properties();
      InitialContext jndiCtx=null;
      try
      {
      props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
      props.put(Context.PROVIDER_URL,"jnp://localhost:1099");
      props.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
      jndiCtx=new InitialContext(props);
      }
      catch(Exception ex)
      {}
      return jndiCtx;
      }

      public static void main(String args[])throws java.io.IOException
      {

      boolean created = false;
      logger = Logger.getRootLogger();
      logger.addAppender(new FileAppender(new HTMLLayout(),
      "mylog1.log",true));
      System.out.println("\nBegin Rels3-Client...\n");
      try
      {

      InitialContext ctx=getInitialContext();

      Object obj = ctx.lookup("AreaSessBean/remote");
      AreaSess sess = (AreaSess)PortableRemoteObject.narrow(obj,AreaSess.class);


      System.out.println("\nGot jndi named object..\n");

      Area area2 = sess.getCodeArea(new Integer(1000));
      System.out.println("area...."+area2);

      System.out.println("......::"+sess.getAreaInfo(new Integer(1000)));

      Bazar bazar = sess.getCodeBazar(new Integer(105));

      System.out.println("....:"+bazar);

      Area area = sess.getBazarArea(bazar);
      System.out.println("Area for bazar id-105 is: "+area);


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

      }

      }


      i made one ant build file as


      <?xml version="1.0" ?>
































































      the oracle sql is
      drop table bazar;

      drop table area;

      drop sequence area_seq;


      drop sequence bazar_seq;


      create sequence bazar_seq;

      create sequence area_seq;


      create table area(id number Constraint area_PK primary key,
      area_name varchar2(20) not null,
      population number);

      create table bazar(id number Constraint bazar_PK primary key,
      bazar_name varchar2(20) not null,
      turnover number,
      AREA_ID number references area(id));



      please look into this and help me to fish out the problem