3 Replies Latest reply on Jan 6, 2002 8:19 PM by garymarsh

    Unabe to Create Entity Bean

    garymarsh

      Hi;

      I am using SQL Server and MS SQLServer JDBC driver. I am able to access the database through a helper method that maintains an autonumbering value for each Entity bean so I know that the JDBC driver and database work. The problem is that after I get the autonumber and apply it to the ejbCreate( long autonumber) for the creation of my entity bean I get the following log message when I run the test program:

      [Default] JBoss 2.4.3 Started in 0m:23s
      [Bean Cache] Resized cache for bean Employee: old capacity = 1000, new capacity = 50
      [Bean Cache] Resized cache for bean Client: old capacity = 1000, new capacity = 50
      [PropertyReader] in readInStream() : propFileName = Paye.properties
      [PropertyReader] URL for propFile is jar:file:/D:/JBoss-2.4.3_Tomcat-3.2.3/jboss/tmp/deploy/Default/Paye_Payroll.jar/ejb1002.jar!/Paye.properties
      [PropertyReader] Properties file was loaded into p
      [PropertyReader] p was set to SystemProperties
      [ClientBean] We have a SeqGenerator object
      [JDBCGenerator] in generate(String seq) : seq =Client
      [JDBCGenerator] in generate(String seq) : conn =com.microsoft.jdbc.sqlserver.SQLServerConnection@5fc672
      [SeqGenerator2] jdbcGen.generate() made new clientId = 13
      [Default] Got an Autogenerated number
      [ClientPK] ClientPK is created : clientId = 13
      [Client] TRANSACTION ROLLBACK EXCEPTION:null
      Embedded Exception
      null; nested exception is:
      javax.ejb.EJBException: null
      Embedded Exception
      null

      ==============

      this is my test program code:

      public static void main(String args[]) throws Exception
      {
      InitialContext iniCtx = null;
      ClientHome home = null;
      ClientRemote bean = null;
      ClientRemote bean2 = null;
      ClientPK pk= null;
      Object ref = null;
      try
      {
      if( (iniCtx = new InitialContext()) == null)
      System.out.println("Initial context is null");
      else
      System.out.println("Got initial context");
      ref = iniCtx.lookup("payroll/Client");
      if( ref == null)
      System.out.println("Reference object is null");
      else
      System.out.println("Got reference object");
      }
      catch( Exception e )
      {
      System.out.println("Initializing error :"+e.getMessage());
      System.exit(1);
      }
      try
      {
      home = (ClientHome) ref;
      bean = home.create("Acme Tools","222 Cabot Rd", "Laguna Niguel", "92677", "TXID-123");
      System.out.println("bean = "+bean.getBeanState());

      pk = (ClientPK)bean.getPrimaryKey();

      bean2 = home.findByPrimaryKey( pk );
      System.out.println("findByPrimaryKey(Bean0): "+bean2);
      bean2.remove();
      bean.remove();
      }
      catch(ObjectNotFoundException e)
      {
      System.out.println("findByPrimaryKey failed as expected");
      System.exit(3);
      }
      catch( Exception e )
      {
      System.out.println("Second catch line :"+e.getMessage());
      System.exit(2);
      }
      .....
      }

      =================

      This is my Entity Bean ejbCreate() code:

      public ClientPK ejbCreate( String name, String address, String city,
      String zip, String taxId) throws CreateException,
      RemoteException
      {
      Long number = null;

      try
      {
      InitialContext ictx = new InitialContext();
      String propFileName = (String)ictx.lookup("java:comp/env/PayeProperties");

      if( this.seqGen == null )
      this.seqGen = new SeqGenerator2(propFileName);

      if( this.seqGen == null )
      log.debug("unable to instanciate a SeqGenerator object");
      else
      log.debug("We have a SeqGenerator object");

      number = new Long(seqGen.generate("Client"));
      if( number != null )
      System.out.println("Got an Autogenerated number");
      }
      catch(javax.naming.NamingException ne)
      {
      ne.printStackTrace();
      }
      catch( Exception e )
      {
      System.out.println(e.getMessage());
      }
      this.clientId = number.longValue();
      this.name = name;
      this.address = address;
      this.city = city;
      this.zip = zip;
      this.taxId = taxId;

      return new ClientPK(this.clientId);
      }
      =========

      This is my ClientPK() code:

      public class ClientPK implements Serializable
      {
      private static final Category log = Category.getInstance(ClientPK.class);

      public long clientId;

      public ClientPK()
      {
      }

      public ClientPK(long clientId)
      {
      this.clientId = clientId;
      log.debug("ClientPK is created : clientId = "+this.clientId);
      }
      public boolean equals(Object obj)
      {
      if (this.getClass().equals(obj.getClass()))
      {
      ClientPK that = (ClientPK) obj;
      return this.clientId == that.clientId;
      }
      return false;
      }
      public int hashCode()
      {
      return new Long(clientId).hashCode();
      }
      }

      ===============

      Can anyone tell me why the create() is failing in my Test program??

      Thanks,

      Gary

        • 1. Re: Unabe to Create Entity Bean
          davidjencks

          I don't see what's wrong but the equals method in your pk will throw a npe if invoked on null.

          • 2. Re: Unabe to Create Entity Bean
            roadrunner

            Hi Gary,

            as far as I know entity beans with CMP must return null for the primary key object -> do not create/Return the ClientPK.

            bye
            Sven

            • 3. Re: Unabe to Create Entity Bean
              garymarsh

              Sven,

              In my first attempt at this I returned null from the ejbCreate()'s and got the same error. In fact here is the log output for the program with null returned as you prescribed and as I initially programed it:

              [PropertyReader] in readInStream() : propFileName = Paye.properties
              [PropertyReader] URL for propFile is jar:file:/D:/JBoss-2.4.3_Tomcat-3.2.3/jboss/tmp/deploy/Default/Paye_Payroll.jar/ejb1002.jar!/Paye.properties
              [PropertyReader] Properties file was loaded into p
              [PropertyReader] p was set to SystemProperties
              [ClientBean] We have a SeqGenerator object
              [JDBCGenerator] in generate(String seq) : seq =Client
              [JDBCGenerator] in generate(String seq) : conn =com.microsoft.jdbc.sqlserver.SQLServerConnection@616483
              [SeqGenerator2] jdbcGen.generate() made new clientId = 17
              [Default] Got an Autogenerated number
              [Client] TRANSACTION ROLLBACK EXCEPTION:null
              Embedded Exception
              null; nested exception is:
              javax.ejb.EJBException: null
              Embedded Exception
              null

              ===============

              looks familiar doesn't it?

              Thanks for the thoughts though,

              Gary