5 Replies Latest reply on Sep 4, 2001 3:01 PM by Anil Tatineni

    CMP doesnt work for me...help...please

    Anil Tatineni Newbie

      ok. i been tryin to get this CMP running for a while.
      i have a simple EJB. I am posting all of my code here.
      can anyone take a look at it and let me know what am i doing wrong?.
      thanks

      My bean class

      //
      // -- Java Code Generation Process --
      
      package junk.test.Test;
      
      // Import Statements
      import javax.ejb.*;
      import java.rmi.RemoteException;
      import javax.ejb.*;
      
      public class TestEJB implements javax.ejb.EntityBean
      {
       public String name;
       public EntityContext EJB_Context;
       public TestPK pk;
      
       public TestEJB ()
       {
      
       }
      
      
       public void ejbActivate ()
       {
      
       }
      
      
      
       public void ejbPassivate ()
       {
      
       }
      
      
       public void ejbLoad ()
       {
      
       }
      
       public void ejbStore ()
       {
      
       }
      
      
       public void ejbRemove () throws javax.ejb.RemoveException
       {
      
       }
      
      
       public void setEntityContext (javax.ejb.EntityContext ctx)
       {
      
       }
      
       public void unsetEntityContext ()
       {
      
       }
      
       public junk.test.Test.TestPK ejbCreate () throws java.rmi.RemoteException, javax.ejb.CreateException
       {
       return null;
       }
      
       public void ejbPostCreate () throws javax.ejb.CreateException
       {
      
       }
      
       public String getname ()
       {
       return name;
       }
      
       public void setname (String name)
       {
       this.name = name;
       }
      
       public junk.test.Test.TestPK getpk ()
       {
       return pk;
       }
      
       public void setpk (junk.test.Test.TestPK pk)
       {
       this.pk = pk;
       }
      }
      

      My remote interface
      //
      // -- Java Code Generation Process --
      
      package junk.test.Test;
      
      // Import Statements
      import javax.ejb.*;
      import java.rmi.RemoteException;
      import javax.ejb.*;
      
      public interface Test extends javax.ejb.EJBObject
      {
      
       public String getname () throws java.rmi.RemoteException;
       public void setname (String name) throws java.rmi.RemoteException;
      
       public junk.test.Test.TestPK getpk () throws java.rmi.RemoteException;
      
       public void setpk (junk.test.Test.TestPK pk) throws java.rmi.RemoteException;
      }
      
      



      my home interface[/]


      //
       // -- Java Code Generation Process --
      
       package junk.test.Test;
      
       // Import Statements
       import javax.ejb.*;
       import java.rmi.RemoteException;
       import javax.ejb.*;
      
       public interface TestHome extends javax.ejb.EJBHome
       {
      
       public junk.test.Test.Test findByPrimaryKey (junk.test.Test.TestPK primaryKey)
       throws java.rmi.RemoteException, javax.ejb.FinderException;
      
       public junk.test.Test.Test create ()
       throws java.rmi.RemoteException, javax.ejb.CreateException;
       }


      My ejb-jar file

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

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

      <ejb-jar>
      <enterprise-beans>

      <ejb-name>TestEJB</ejb-name>
      junk.test.Test.TestHome
      junk.test.Test.Test
      <ejb-class>junk.test.Test.TestEJB</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>junk.test.Test.TestPK</prim-key-class>
      False
      <primkey-field>pk</primkey-field>
      <cmp-field><field-name>pk</field-name></cmp-field>
      <cmp-field><field-name>name</field-name></cmp-field>

      </enterprise-beans>
      </ejb-jar>



      My jboss.xml


      <?xml version="1.0" encoding="Cp1252"?>


      false
      <container-configurations />
      <resource-managers />
      <enterprise-beans>

      <ejb-name>TestEJB</ejb-name>
      <jndi-name>test/Test</jndi-name>
      <configuration-name></configuration-name>

      </enterprise-beans>



      My Jaws.xml file



      java:/OracleDB
      <type-mapping>Oracle8</type-mapping>
      <enterprise-beans>

      <ejb-name>TestEJB</ejb-name>
      <table-name>Test</table-name>
      <remove-table>false</remove-table>
      <create-table>false</create-table>

      <cmp-field>
      <field-name>pk.id</field-name>
      <column-name>id</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>name</field-name>
      <column-name>name</column-name>
      </cmp-field>



      </enterprise-beans>


      MY primary key

      //
      // -- Java Code Generation Process --
      
      package junk.test.Test;
      
      // Import Statements
      import java.io.Serializable;
      
      public class TestPK implements java.io.Serializable
      {
      
       public int id;
       public int hashCode ()
       {
       return 0;
       }
      
       public boolean equals ()
       {
       return true;
       }
      
       public java.lang.String toString ()
       {
       return "" + id; ;
       }
      
       public TestPK ()
       {
      
       }
      
       public int getid()
       {
       return id;
       }
      
       public void setid(int id)
       {
       this.id = id;
       }
      }
      


      I am using pspy to log the sql being generated. this is what see in the log when fire a client for this ejb.
      SELECT COUNT(*) FROM Test WHERE id=?|SELECT COUNT(*) FROM Test WHERE id='[B@179f67'

      clearly the id is being equaled to the string representation of primary key object.
      I followed the docs assuming that the primary key class is dependent class and included the pk.id in the attributes in jaws.xml.

      can some one please help



        • 1. Re: CMP doesnt work for me...help...please
          Ian Butcher Newbie

          In your ejbCreate you have to pass in any values that you want to assign to your entity. I see from your code that you have name and a pk object, I think what you want is:

          public TestPK ejbCreate ( TestPK pk, String name )
          throws CreateException {

          this.pk = pk;
          this.name = name;

          return null;
          }

          don't forget to put in a matching ejbPostCreate.

          • 2. Re: CMP doesnt work for me...help...please
            Anil Tatineni Newbie

            i am just tryin to lookup the bean in my client. not even tryin to create. that doesnt work. and included the method as suggested but that doesnt work either.
            still gives me the following query

            SELECT COUNT(*) FROM Test WHERE id='[B@13bc1'

            • 3. Re: CMP doesnt work for me...help...please
              Ronaldo Nascimento Newbie

              Looks like your primary key is your own Class, and not one of java.lang types. So what is happening is the container is asking your class for the toString() method, and it is returning the default from java.lang.Object.

              Instead, your PK class needs to override a toString method which will return a form that can be used in SQL statements.

              • 4. Re: CMP doesnt work for me...help...please
                Paul Austin Newbie

                The test PK must implement proper verdsions of hashCode and equals, the versions you have will not work at all. For two objects that are equal they must have the same hashCode. The reason for this is that hasCode and equals are used in the insertion and retreival from hash tables which are used for the caching in ejb containers.

                For your class you might as well use the java.lang.Integer class as you are only using an int primary key and it will save you a lot of work. If not the following methods will do it for you.

                public int hashCode() {
                return id;
                }

                public boolean equals(Object o) {
                if (o != null && o instanceof TestPK) {
                if (id == ((TestPK)o).id) {
                return true;
                }
                }
                return false;
                }

                Also using "" + id to convert an int to a String is a big no no use String.valueOf(id) instead as the + operator is compiled to StringBuffer append operations.

                Paul

                • 5. Re: CMP doesnt work for me...help...please
                  Anil Tatineni Newbie

                  i did exactly as u asked me to do. it still doesnt work.

                  my new primary key class looks like this.


                  // your java code here
                  
                  package junk.test.Test;
                  import java.io.Serializable;
                  
                  public class TestPK implements java.io.Serializable
                  {
                  
                   public int id;
                  
                   public int hashCode()
                   {
                   return id;
                   }
                  
                   public boolean equals(Object o)
                   {
                   if (o != null && o instanceof TestPK)
                   {
                   if (id == ((TestPK)o).id)
                   {
                   return true;
                   }
                   }
                   return false;
                   }
                  
                  
                  
                  
                   public java.lang.String toString ()
                   {
                   return String.valueOf(id) ;
                   }
                  
                   public TestPK ()
                   {
                  
                   }
                  
                   public int getid()
                   {
                   return id;
                   }
                  
                   public void setid(int id)
                   {
                   this.id = id;
                   }
                  }
                  



                  but then again i tried just the Integer class. which worked fine.
                  the log statements look like this

                  with my primary key class:-
                  999629177957|SELECT COUNT(*) FROM Test WHERE pk=?|SELECT COUNT(*) FROM Test WHERE pk='[B@3a4159'


                  with integer class it looked like
                  999628557836|SELECT name,id FROM Test WHERE id=?|SELECT name,id FROM Test WHERE id='1'
                  (this is the one which worked).

                  when u look at the sql statements the where clause is for "pk" in the first case and for "id" in the second case.
                  where id is my database field. so is there something goin on wrong with my deployment??