Can Entity Beans implement interfaces?

Discussions

EJB design: Can Entity Beans implement interfaces?

  1. Can Entity Beans implement interfaces? (2 messages)

    I have been studying Java for several months now for a project I may be working on. It may be a Web app using servlets, JSP pages, Session and Entity Beans. One of the designs I have come up with would call for the Entity Beans to implement an interface. With this design, a generic business method within a Session Bean can act on any one of the Entity Beans that implement this interface without knowing or caring which type of Entity Bean it is. What I haven't been able to determine from my reading is if Entity Beans can implement interfaces other than the normal bean interface.
  2. You can most definitely implement interfaces in Entity Beans in EJB 1.1. Not sure about EJB 2.0 , though.

    There is a section in the book "Enterprise Javabeans 3rd Edition by Richard Monson-Haefel (O'Reilly)" page 463 under the topic "EJB 1.1 : The Business Interface Alternative" which discusses this. Pages 460 - 464 will be a good and helpful read.
  3. What do you mean by "entity bean"? The session bean would be manipulating a *remote instance* of the entity bean -- it's already an Interface, and is passed and manipulated as one.

    So, an entity bean called "entity" has three objects which you write. "Entity" the interface which extends EJBObject, "EntityEJB" which doesn't even (and shouldn't) implement this interface but provides the server side execution for it, and "EntityHome" the home interface.

    The session bean gets a reference to a generated remote stub object which is the implementation of the "Entity" interface.

    What you normally have to do it write a 'Business Interface' which is just a plain old interface which doesn't extend any of the EJB interfaces. Then the remote interface, which extends EJBObject interface, should also extend the Business Interface. The actual bean object should also extend the business interface. This pattern is the 'Business Interface' pattern as found in Floyd's Book, 'EJB Design Patterns' (page 40).

    What you might be able to do is modify this slighty. Write a 'Super business interface' which all your business interfaces then extend.

    The problem I see there immediately is that if you have a method;

    <pre>
    someMethod(SuperBusinessInterface sbi) {
        // some code here
    }
    </pre>

    You can't be sure that the 'SuperBusinessInterface' is actually also a remote stub. I suppose that might not matter as long as you can separate this method from having to do anything to do with the EJB interface;

    <pre>
    someSessionMethod(String aString) {
       // get the remote stub
       Entity e = getEntityHome().findByPrimaryKey(
           new EntityKey(aString));
       /*
        * Entity remote interface is known to
        * extend EntityBusinessInterface which
        * extends SuperBusinessInterface.
        */
        someMethod(e);
        /*
         * someMethod can't call remove() because
         * SuperBusinessInterface doesn't extend
         * any of the EJB itnerfaces, so we call
         * it here instead.
         */
        e.remove();

    }
    </pre>

    That's an approximate idea and I wouldn't be surprised if there are a million design reasons to not do it so you should do a little more research into it before implementing it.

    regs
    scot.