CMP with a Primary Key Class

Discussions

EJB programming & troubleshooting: CMP with a Primary Key Class

  1. CMP with a Primary Key Class (7 messages)

    I am testing CMP with a Primary Key Class which source code given below, the problem that I have is when I am calling the Business Logic (getUserId()) an exception is generated. I am using j2sdkee1.3 .Can any one please tell me where I am making the mistake. Where do I have to change to the Primary Key Class whith a BMP.
    Thanx in ADV ,

    -------------------------------------------
    import java.util.Collection;
    import java.rmi.RemoteException;
    import javax.ejb.*;
    import testCMP;
    import testPK;

    public interface testCMPHome extends EJBHome {

       public testCMP create(String UserID,String SessionID)
    throws RemoteException, CreateException;

        public testCMP findByPrimaryKey(testPK PrimaryKey)
            throws FinderException, RemoteException;
      
    }
    -------------------------------------------------

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

    public interface testCMP extends EJBObject
    {
         public String getUserId() throws RemoteException;
     
    }


    -------------------------------------------
    import java.util.*;
    import javax.ejb.*;
    import testPK;

    public class testCMPEJB implements EntityBean {
       
       public String strUserID;
       public String strSessionID;
      
       private EntityContext context;

        public String getUserId() {
    System.out.println("Business Logic Called");
          return strUserID;
       }




    public testPK ejbCreate(String UserID,String SessionID)
        throws CreateException {
    this.strUserID=UserID;
    this.strSessionID=SessionID;
          return null;
       }

       public void setEntityContext(EntityContext context) {

          this.context = context;
       }

     
       public void unsetEntityContext() {
          this.context = null;
       }

     
       public void ejbActivate() {

          strSessionID = (String)context.getPrimaryKey();
       }

       public void ejbPassivate() {
          strSessionID = null;
          strUserID = null;
       }



       public void ejbRemove() { }
       public void ejbLoad() { }
       public void ejbStore() { }
       public void ejbPostCreate(String UserID,String SessionID) {}

    }

    -------------------------------------------

    import java.io.*;
    import java.util.*;

    public class testPK implements java.io.Serializable {

    public String strSessionID;


    public testPK() {}

    public testPK(String SessionID)
    {
     this.strSessionID = SessionID;
    }


    public String getSessionID(){
    return strSessionID;
    }



    public int hashCode()
    {
     StringBuffer strBuff = new StringBuffer( );
     strBuff.append(strSessionID);
     String str = strBuff.toString();
     int hashCode = str.hashCode( );
     return hashCode;


    }


     public boolean equals(Object other)
        {
            if (other instanceof testPK)
            {
            return ( strSessionID.equals(((testPK)other).strSessionID));
            }
            return false;
      }
         



    }

    -------------------------------------------
    import java.util.*;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.rmi.PortableRemoteObject;
    import testCMPHome;
    import testCMP;
    import testPK;

    public class testCMPClient {

       public static void main(String[] args) {

           try {
               Context initial = new InitialContext();
               Object objref = initial.lookup("MytestCMP");

    testCMPHome home =(testCMPHome) PortableRemoteObject.narrow(objref,testCMPHome.class);
              testCMP test = home.create("86","12");
              String ans=test.getUserId();
               System.out.println(ans);
             
               

              
           } catch (Exception ex) {
               System.err.println("Caught an exception." );
               ex.printStackTrace();
           }
       }
    }
    -------------------------------------------
    Error at Server Side

    javax.ejb.EJBException: nested exception is: java.lang.ClassCastException: testP
    K
            at com.sun.ejb.containers.EntityContainer.getReadyEJB(EntityContainer.ja
    va:1326)
            at com.sun.ejb.containers.EntityContainer.getContext(EntityContainer.jav
    a:211)
            at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:336
    )
            at testCMPEJB_PM_EJBObjectImpl.getUserId(testCMPEJB_PM_EJBObjectImpl.jav
    a:19)
            at _testCMPEJB_PM_EJBObjectImpl_Tie._invoke(Unknown Source)
            at com.sun.corba.ee.internal.POA.GenericPOAServerSC.dispatchToServant(Ge
    nericPOAServerSC.java:514)
            at com.sun.corba.ee.internal.POA.GenericPOAServerSC.internalDispatch(Gen
    ericPOAServerSC.java:199)
            at com.sun.corba.ee.internal.POA.GenericPOAServerSC.dispatch(GenericPOAS
    erverSC.java:107)
            at com.sun.corba.ee.internal.iiop.ORB.process(ORB.java:272)
            at com.sun.corba.ee.internal.iiop.RequestProcessor.process(RequestProces
    sor.java:84)
            at com.sun.corba.ee.internal.orbutil.ThreadPool$PooledThread.run(ThreadP
    ool.java:99)
    java.lang.ClassCastException: testPK
            at testCMPEJB.ejbActivate(testCMPEJB.java:50)
            at com.sun.ejb.containers.EntityContainer.getReadyEJB(EntityContainer.ja
    va:1317)
            at com.sun.ejb.containers.EntityContainer.getContext(EntityContainer.jav
    a:211)
            at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:336
    )
            at testCMPEJB_PM_EJBObjectImpl.getUserId(testCMPEJB_PM_EJBObjectImpl.jav
    a:19)
            at _testCMPEJB_PM_EJBObjectImpl_Tie._invoke(Unknown Source)
            at com.sun.corba.ee.internal.POA.GenericPOAServerSC.dispatchToServant(Ge
    nericPOAServerSC.java:514)
            at com.sun.corba.ee.internal.POA.GenericPOAServerSC.internalDispatch(Gen
    ericPOAServerSC.java:199)
            at com.sun.corba.ee.internal.POA.GenericPOAServerSC.dispatch(GenericPOAS
    erverSC.java:107)
            at com.sun.corba.ee.internal.iiop.ORB.process(ORB.java:272)
            at com.sun.corba.ee.internal.iiop.RequestProcessor.process(RequestProces
    sor.java:84)
            at com.sun.corba.ee.internal.orbutil.ThreadPool$PooledThread.run(ThreadP
    ool.java:99)

    -------------------------------------------
    Error at Client Side



    java.lang.NullPointerException
            at java.util.Hashtable.get(Hashtable.java, Compiled Code)
            at com.sun.corba.ee.internal.io.FVDCodeBaseImpl.connect(FVDCodeBaseImpl.
    java:183)
            at com.sun.corba.ee.internal.iiop.IIOPConnection.getFVD(IIOPConnection.j
    ava:1065)
            at com.sun.corba.ee.internal.io.FVDCodeBaseDelegate.meta(FVDCodeBaseDele
    gate.java:85)
            at com.sun.corba.ee.internal.io.IIOPInputStream.getOrderedDescriptions(I
    IOPInputStream.java:821)
            at com.sun.corba.ee.internal.io.IIOPInputStream.inputObjectUsingFVD(IIOP
    InputStream.java:978)
            at com.sun.corba.ee.internal.io.IIOPInputStream.simpleReadObject(IIOPInp
    utStream.java:213)
            at com.sun.corba.ee.internal.io.ValueHandlerImpl.readValueInternal(Value
    HandlerImpl.java:234)
            at com.sun.corba.ee.internal.io.ValueHandlerImpl.readValue(ValueHandlerI
    mpl.java:164)
            at com.sun.corba.ee.internal.iiop.CDRInputStream.read_value(CDRInputStre
    am.java:872)
            at com.sun.corba.ee.internal.core.UEInfoServiceContext.<init>(UEInfoServ
    iceContext.java:33)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at com.sun.corba.ee.internal.core.ServiceContextData.makeServiceContext(
    ServiceContextData.java:113)
            at com.sun.corba.ee.internal.core.ServiceContexts.<init>(ServiceContexts
    .java:106)
            at com.sun.corba.ee.internal.iiop.ReplyMessage.read(ReplyMessage.java:13
    3)
            at com.sun.corba.ee.internal.iiop.IIOPInputStream.<init>(IIOPInputStream
    .java:88)
            at com.sun.corba.ee.internal.iiop.ClientResponseImpl.<init>(ClientRespon
    seImpl.java:28)
            at com.sun.corba.ee.internal.iiop.IIOPConnection.createInputStream(IIOPC
    onnection.java:622)
            at com.sun.corba.ee.internal.iiop.ReaderThread.run(IIOPConnection.java:1
    09)
    Caught an exception.
    org.omg.CORBA.UNKNOWN: minor code: 0 completed: Maybe
            at com.sun.corba.ee.internal.core.UEInfoServiceContext.<init>(UEInfoServ
    iceContext.java:37)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at com.sun.corba.ee.internal.core.ServiceContextData.makeServiceContext(
    ServiceContextData.java:113)
            at com.sun.corba.ee.internal.core.ServiceContexts.<init>(ServiceContexts
    .java:106)
            at com.sun.corba.ee.internal.iiop.ReplyMessage.read(ReplyMessage.java:13
    3)
            at com.sun.corba.ee.internal.iiop.IIOPInputStream.<init>(IIOPInputStream
    .java:88)
            at com.sun.corba.ee.internal.iiop.ClientResponseImpl.<init>(ClientRespon
    seImpl.java:28)
            at com.sun.corba.ee.internal.iiop.IIOPConnection.createInputStream(IIOPC
    onnection.java:622)
            at com.sun.corba.ee.internal.iiop.ReaderThread.run(IIOPConnection.java:1
    09)




    Threaded Messages (7)

  2. CMP with a Primary Key Class[ Go to top ]

    When using a CMP, your ejbCreate() method of the bean should not return anything, i.e. void. In your case, you are returning the Primary Key class.
  3. CMP with a Primary Key Class[ Go to top ]

    Adnan
    the return value of the ejbCreate method should be primary key but u should actually return null for CMPs.
    Tim
  4. CMP with a Primary Key Class[ Go to top ]

    Agree. I think I did not make myself clear in my last msg. What meant to say was that the method should be declared like this:
    public void ejbCreate() throws....

    Of course, the method's code would still be the same for BMPs or CMPs. Thanks for pointing that out.
  5. CMP with a Primary Key Class[ Go to top ]

    I must be day-dreaming today! The ejbCreate() methods in CMPs and BMPs are very different. Moreover, I disagree with your last comment that your CMP ejbCreate() should return null. Since, the return type is void, your code doesn't need to return anything, not even a null value. However, you could simply use a "return;" at the end if you like.
  6. CMP with a Primary Key Class[ Go to top ]

    The correct syntax for this method is noted in the spec.

    public MyBeanPK ejbCreate(....)
    {

       // Do things.
       return null;
    }


    WebLogic will usually just moan at you if you don't do this.

    The method signature has been like this since 1.1. Only in 1.0 did ejbCreate() return void.

    Chz

    Tony
  7. CMP with a Primary Key Class[ Go to top ]

    Hi guys thank you very much for your prompt advices. I have tried all them out and came with a solution. I am providing the source codes of the changed classes for future references for any person. Keep up your good work (Adnan,Tim & Tony).
    Dinesh.
    import java.util.*;
    import javax.ejb.*;
    import testPK;

    public class testCMPEJB implements EntityBean {
       
       public String strUserID;
       public String strSessionID;
      



       private EntityContext context;



        public String getUserId() {
    System.out.println("Business Logic Called");
          return strUserID;
       }




    public testPK ejbCreate(String UserID,String SessionID)
        throws CreateException {
    this.strUserID=UserID;
    this.strSessionID=SessionID;
    System.out.println("Object Created");
          return null;
       }




       public void setEntityContext(EntityContext context) {

          this.context = context;
       }

     
       public void unsetEntityContext() {
          this.context = null;
       }
     

       public void ejbActivate() { }
       public void ejbPassivate() { }
       public void ejbRemove() { }
       public void ejbLoad() { }
       public void ejbStore() { }
       public void ejbPostCreate(String UserID,String SessionID) {}

    }
    -----------------------------------------------------------------------
    import java.io.*;
    import java.util.*;

    public class testPK implements java.io.Serializable {

    public String strSessionID;
    public String strSessionID;

    public testPK() {}

    public testPK(String SessionID)
    {
     this.strSessionID = SessionID;
    }


    public String getSessionID(){
    return strSessionID;
    }





     public boolean equals(Object other)
        {
            if (other instanceof testPK)
            {
            return ( strSessionID.equals(((testPK)other).strSessionID));
            }
            return false;
      }




     public int hasCode()
        {
            return this.toString().hashCode();
      }





    public String toString()
    {
     String strConcat = new String(strSessionID);
     return strConcat;
    }





    }
     
    -----------------------------------------------
    import java.util.*;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.rmi.PortableRemoteObject;
    import testCMPHome;
    import testCMP;
    import testPK;

    public class testCMPClient {

       public static void main(String[] args) {

           try {
               Context initial = new InitialContext();
               Object objref = initial.lookup("MytestCMP");

    System.out.println("Home Before");
               testCMPHome home =(testCMPHome) PortableRemoteObject.narrow(objref,testCMPHome.class);

             // testCMP duke = home.create("87","13"); //First uncomment this
    testCMP test = home.findByPrimaryKey(new testPK ("13"));
           String ans=test.getUserId();
              System.out.println(ans);
             
               

              
           } catch (Exception ex) {
               System.err.println("Caught an exception." );
               ex.printStackTrace();
           }
       }
    }



  8. CMP with a Primary Key Class[ Go to top ]

    Tiny point, but your primary key equals method has a potential hole in it.

    You can't use instanceof UNLESS your class is final.

    Otherwise, there could be a subclass of your type. This would also return true under the instanceof call.

    Chz

    Tony

    PS. Glad you found a solution.