Define entity beans that allow both remote and local access

Discussions

EJB programming & troubleshooting: Define entity beans that allow both remote and local access

  1. Hi everyone, i have a question when developing my entity beans.

    In an EJB JAR file, i provide several CMP entity beans and a session bean. The session bean acts as a facade for my entity beans, so i want the session bean to access my entity beans via local interfaces. But user may want to have access
    directly to the entity beans when necessary. So i'd like to provide both local interfaces and remote interfaces for my entity beans.

    If i have a single entity bean, everything goes well. But if i have several entity beans which hold relationships to each others, it seems to be troublesome.

    For example, my Account entity bean has the following remote interfaces and local interfaces:

    home: foo.AccountHome
    remote: foo.Account
    local-home: foo.LocalAccountHome
    local: foo.LocalAccount
    ejb-class: foo.AccountBean

    The Transaction bean can navigate to the Account bean to which this transaction belongs to. Following is the code fragment in my TransactionBean class:

    public abstract class TransactionBean implements EntityBean {
      ... ...
      // CMR Field Accessors ...
      public abstract Account getAccount();
      public abstract void setAccount(Account acct);
      // public abstract LocalAccount getAccount();
      // public abstract void setAccount(LocalAccount acct);
      ... ...
    }



    The getAccount() method returns an instance of Account's remote interface, and the setAccount(..) method takes as parameter an instance of Account's remote interface. But what will happen to my session bean who will access the
    Transaction bean via the local interface and local home interface? It is expecting the methods which are commented.

    Since the CMR field accessors take parameters or return values that are instances of another beans remote or local interface, they must know which interface to use: remote interface or local. Is that to say, when coding entity
    beans with relationships, i must decide what kind of entity beans they are? They may allow remote invocation, or local invocation, but never both?

    Thanks.
    heavyz
  2. Entity beans should only expose Local interfaces for reasons of efficiency.

    The usual work around is to wrap them in a facade (which you have done) and have the facade expose remote methods.

    In order for this to work you will need to create a VO/DTO which contains the information stored in the entity bean.

    Not very nice, but used to get around network inefficiencies.

    If you *really* must expose a remote method on the entity bean, have a

      EntityVO getDetails() {
      }

    which returns a VO/DTO. Just don't ask any EJB2.0 people to endorse it :)
  3. Thank you very much for your advice, Colin. What you said is what i did in practice. But what i want to know is not the "best practice" but the possibility to expose both remote interface and local interface of an entity bean.

    I agree that "Entity beans shouldn't expose remote interfaces". But i still wonder if it is true that "Entity beans cannot expose both remote interface and local interface"..

    heavyz.
  4. There is nothing in the EJB spec that prevents them, they are after all just Enterprise Java Beans ;)