3 Replies Latest reply on Jul 8, 2002 10:47 AM by Simon Hutchinson

    CMR issue when using findByPrimaryKey

    Simon Hutchinson Newbie

      Hi,

      EJB 2.0
      Jboss 3.0

      I am fairly new to EJBs so forgive me if this is a ridiculous question.
      I am trying to code a One-To-Many Bidirectional Relationship between two entity beans.
      User is the One side of the relationship and Calls the Many side.

      I need to create calls Remotely and so have coded a Session bean to handle the creation
      of calls.

      The following code snippet, which I used for testing creates a new user and then a new call for that user
      the CMR works perfectly well and the foriegn key is stored in the Calls db Table


      public void newCall(String userName, String shortDescription)
      throws RemoteException {

      Context context = new InitialContext();
      //Create a test user
      UserLocalHome userHome = (UserLocalHome) context.lookup("local/UserBean");
      UserLocal userLocal = userHome.create(userName);

      //Create a test call for the user by passing in the Local interface
      CallLocalHome callHome = (CallLocalHome)context.lookup("local/CallBean");
      CallLocal call = callHome.create(shortDescription,userLocal);

      .......
      }


      However what I actually want to be able to do is to Simply provide the Primary key of an
      existing user to the method, look up the Local interface and then create the call for the User.

      I have used the following code in the same session bean as the code above.

      public void newCall(Integer userId, String shortDescription)
      throws RemoteException {

      Context context = new InitialContext();
      //Get UserLocal interface from Primary key
      UserLocalHome userHome = (UserLocalHome)context.lookup("local/UserBean");
      UserLocal userLocal = (UserLocal)userHome.findByPrimaryKey(userId);

      //Create a test call for the user by passing in the Local interface
      CallLocalHome callHome = (CallLocalHome)context.lookup("local/CallBean");
      CallLocal call = callHome.create(shortDescription,userLocal);

      .......
      }


      The following is the Jboss console output after this code runs

      15:49:51,909 DEBUG [CallBean] Create: pk=9
      15:49:51,909 DEBUG [CallBean] Executing SQL: SELECT COUNT(*) FROM CALLS WHERE CALL_ID=?
      15:49:52,009 DEBUG [CallBean] Executing SQL: INSERT INTO CALLS (CALL_ID, DATE_CR
      EATED, SHORT_DESCRIPTION, LONG_DESCRIPTION, PRIORITY, user_Id) VALUES (?, ?, ?,
      ?, ?, ?)
      15:49:52,109 DEBUG [CallBean] Rows affected = 1
      15:49:52,109 DEBUG [UserBean] Executing SQL: SELECT CALL_ID FROM CALLS WHERE (us
      er_Id=?)
      15:49:52,119 ERROR [LogInterceptor] TransactionRolledbackException, causedBy:
      java.sql.SQLException: Table not found: CALLS in statement [SELECT CALL_ID FROM
      CALLS WHERE (user_Id=42)]
      at org.hsqldb.Trace.getError(Trace.java:180)
      at org.hsqldb.Result.(Result.java:175)
      at org.hsqldb.jdbcConnection.executeHSQL(jdbcConnection.java:907)
      at org.hsqldb.jdbcConnection.execute(jdbcConnection.java:718)
      at org.hsqldb.jdbcStatement.fetchResult(jdbcStatement.java:686)
      at org.hsqldb.jdbcStatement.executeQuery(jdbcStatement.java:68)
      at org.hsqldb.jdbcPreparedStatement.executeQuery(jdbcPreparedStatement.j
      ava:133)


      This seems a little strange as the output shows the INSERT is successful but the following
      SELECT statement fails.

      My remote client also catches the following exception

      java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
      java.rmi.ServerException: Error in addRelation


      I am little confused as both snippets above are passing the UserLocal interface to the same method

      Can anybody shed any light on this for me

      Si

        • 1. Re: CMR issue when using findByPrimaryKey
          Dain Sundstrom Master

          This is weird. Are you sure CALLS is not a reserved word in your database. Are using different datasources for the CallsBean and UserBean? I suggest you try changing the table name first.

          If you can't get it to work post bug report at sourceforge with a simple (small) reproducable testscase.

          • 2. Re: CMR issue when using findByPrimaryKey
            Simon Hutchinson Newbie

            Hi there

            <Are you sure CALLS is not a reserved word in your database.>

            Definitely not. The console output from my original posting shows that the initial INSERT into the CALLS table was OK.


            15:49:52,009 DEBUG [CallBean] Executing SQL: INSERT INTO CALLS (CALL_ID, DATE_CR
            EATED, SHORT_DESCRIPTION, LONG_DESCRIPTION, PRIORITY, user_Id) VALUES (?, ?, ?,
            ?, ?, ?)
            15:49:52,109 DEBUG [CallBean] Rows affected = 1

            <Are using different datasources for the CallsBean and UserBean? >

            This occured to me, but wouldn't explain why the create works when utilising a Local interface not returned from the findByPrimaryKey() method.
            I double checked anyway and both are using the same Datasource.


            <If you can't get it to work post bug report at sourceforge with a simple (small) reproducable testscase. >

            Will do and thanks for your reply

            Si

            • 3. Re: CMR issue when using findByPrimaryKey
              Simon Hutchinson Newbie

              My mistake

              I hadn't included the


              java:/MyDataSourceName


              tag

              in jbosscmp-jdbc.xml

              The relationship was using the DefaultDS even though the
              entities were configured to use the correct datasource.

              Everything is working fine now

              Si