3 Replies Latest reply on Nov 4, 2002 2:20 AM by ijcd

    Problem with finders, and proxy wrapping of getPrimaryKey()

    doogie


      /*
      * Code block 1(the bean, which implements a custom finder,
      * findByTemplate
      */
      public class CommunityBean
      extends CommunityImpl
      implements javax.ejb.EntityBean
      {
      ....
      public Collection ejbFindByTemplate(Object template)
      throws RemoteException, FinderException
      {
      Vector results = new Vector();
      EJBCommunity community = null;
      Community communityTemplate = (Community)template;
      CommunityHome communityHome = (CommunityHome)ctx.getEJBHome();
      try {
      System.err.println( "looking for '" + communityTemplate.getId() + "'" );
      community = communityHome.findByPrimaryKey( communityTemplate.getId() );
      System.err.println( "found existing community, key = '" + community.getPrimaryKey() + "'" );
      results.addElement( community );
      } catch ( RemoteException e ) {
      e.printStackTrace();
      throw e;
      } catch ( FinderException e ) {
      e.printStackTrace();
      }
      return results;
      }
      }

      /*
      * Code block 2, a generic import class. It iterates over a vector of
      * templates, requesting existing objects, or if no existing object is
      * found, creating a new one, and then applies the template values
      * onto the existing or new object.
      */
      ...
      public EJBObject importObject( Object object )
      throws Exception
      {
      ...
      Object ref = jndiContext.lookup( jndiName );
      TemplateFinder finder = (TemplateFinder)PortableRemoteObject.narrow( ref, TemplateFinder.class );
      System.err.println( "calling findByTemplate" );
      Object newObject = null;
      Collection list = finder.findByTemplate( object );
      System.err.println( list.size() );
      if ( list.size() == 1 )
      return (EJBObject)list.iterator().next();
      /*
      * Code block 3, the wrapper that calls code block 2.
      */
      ...
      EJBObject ejb = importObject( object );
      ...
      System.err.println( "key for object was '" + ejb.getPrimaryKey() + "'(" + key.getClass().getName() + ")" );
      =============================
      Code block 3 calls code block 2, which calls code block 1. Please look at
      the following debug print statements(and follow along in the code above),
      and tell me if you see the problem.


      ===
      [21:09:17,022,Default] importing com.kbhome.web.ejb.community.CommunityImpl@41f227,
      [21:09:17,053,Default] calling findByTemplate
      [21:09:17,054,Default] looking for '00600600'
      [21:09:17,055,Default] found existing community, key = '00600600'
      [21:09:17,078,Default] 1
      [21:09:17,078,Default]
      [21:09:17,132,Default] key for object was 'CommunityBean:00600600'($Proxy20)
      ===

      Note out the inner println, prints the primary key as '00600600', but the
      outer println, has the primary key munged.

      The problem is that the proxy class is prepending the bean name to the
      primary key. In fact, the actual call to getPrimaryKey() returns a proxy,
      and NOT the key class(which is java.lang.String).

      This problem occurs on JBoss 2.4.4beta.

        • 1. Re: Problem with finders, and proxy wrapping of getPrimaryKe
          doogie

          Here's a little more debug, that truly shows this is a bug in jboss.

          ==
          EJBObject proxyObject = EJBObject)list.iterator().next();
          System.err.println( proxyObject.getClass() + "/" + proxyObject );
          Object key = proxyObject.getPrimaryKey();
          System.err.println( key.getClass() + "/" + key );
          ==
          [21:47:14,742,Default] class $Proxy69/CommunityBean:CommunityBean:00600600
          [21:47:14,742,Default] class $Proxy69/CommunityBean:00600600

          Note how the first print shows a doubled CommunityBean:CommunityBean. If someone says this is a valid bug, I can come up with a patch.

          • 2. Re: Problem with finders, and proxy wrapping of getPrimaryKe
            doogie

            I've worked around this for now, with what is probably very evil code.

            Note the double cast below.
            ==
            EJBObject proxyObject = EJBObject)list.iterator().next();
            System.err.println( proxyObject.getClass() + "/" + proxyObject );
            EJBObject key = (EJBObject)proxyObject.getPrimaryKey();
            System.err.println( key.getClass() + "/" + key.getPrimaryKey() );

            return key;

            ==
            [21:59:37,875,Default] class $Proxy83/CommunityBean:CommunityBean:00600600
            [21:59:37,875,Default] class $Proxy83/00600600
            ==

            • 3. Re: Problem with finders, and proxy wrapping of getPrimaryKe
              ijcd

              I just spent 24 hours getting bitten by this. Do you know if this is fixed in newer versions?