4 Replies Latest reply on Feb 13, 2004 5:01 AM by tsangcn

    EJB References

    Klaus Kaal Newbie

      Hi,

      I like to reference to beans to be able for them to access each other. I understand that I am not allowed to hand over "this" as Parameter to the other bean.

      Object o = jndiContext.lookup( "somewhat" );
      (MyBeanHomeLocal) mhl = (MyBeanHomeLocal)PortableRemoteObject.narrow( o, MyBeanHomeLocal.class );

      //would this creator possible?????
      MyBean mb = mhl.create( this );

      Or, how do I make it possible that MyBean would access the actual one??

      Thanks for your helk

      Klaus

        • 1. Re: EJB References
          Darran Lofthouse Master

          First you do not use PortableRemoteObject.narrow on a local interface.

          To pass a reference of your bean :-

          Implement the 'setSessionContext' method so that you cache an instance of SessionContext each time it is called.

          Assuming you have a member variable called context to hold the reference to the SessionContext you can call context.getEJBLocalObject() to get access to the local interface of your session bean - this is what you are allowed to pass round.


          • 2. Re: EJB References
            Klaus Kaal Newbie

            Fine, understood this (not too many books talk about that :-) )

            But how do I get from the EJBLocalObject to my Localinterface. Do I cast the EJBLoclObject?

            In other words: If I had in my class a EJBLocalObject reference handed in from say interface UserManager.

            ------------------------------------------
            EJBLocalObject um;

            public void setUm( EJBLocalObject elo ){
            um = elo;
            }
            ------------------------------------------
            //would I use it by just casting it the right way?

            valuewhatever = ((UserManager) um).getMyVal();
            ------------------------------------------

            I hope, you understand what I mean.

            Thanks again for your help.

            Klaus

            • 3. Re: EJB References
              marc fleury Master

              I am confused about the "this" reference.

              I am asking because I believe we could simplify the way beans are hooked up in EJB 3.0.

              Please describe what kind of bean you are talking about (session, entity, stateful) and what you want your client code to look like.

              • 4. Re: EJB References
                tsangcn Newbie

                BE CAREFUL when use context.getEJBLocalObject() in JBoss 3.2.1. It seems that there is an intermittent bug in JBoss 3.2.1. I have experienced that context.getEJBLocalObject() sometimes returns a WRONG reference. Someone in the forum also talk about this problem. Use context.getPrimaryKey() to get the pk and find the local reference is more secure.