I am doing something very similar to what you described.  My base class is
actually coded like a Entity bean so that it has all the ejbActivate,
ejbLoad, ejbStore etc.  Hence it is a self-contained entity bean which
handles its own state information nicely and my subclasses are not required
to re-declare those dummy functions if they do not need to.

Something like the following: -

abstract class MyBase implements EntityBean {
        protected transient EntityContext ctx;

        Vector states;

        /** My pseudo ejbCreate function */
        public void ejbCreate() { 
                states = new Vector();
        }

        public void setEntityContext(EntityContext ctx) { this.ctx = ctx; }

        public void unsetEntityContext() { this.ctx = null; }

        public void ejbActivate() {
                states = new Vector();
        }

        public void ejbPassivate() {
                states = null;
        }

        public void ejbRemove() {
                stats = null;
        }

        public void ejbLoad() { }

        public void ejbStore() { }
}

public class MySubclass extends MyBase implements EntityBean {

        String value;
        
        public Integer ejbCreate(String value) {
                super.ejbCreate();
        
                this.value = value;

                return null;
        }
}

Conrad

-----Original Message-----
From: Randahl Fink Isaksen [mailto:[EMAIL PROTECTED]]
Sent: Monday, February 19, 2001 4:18 PM
To: Orion-Interest
Subject: Best practices: How to initialize state in EntityBean's
superclass


Suppose you have two EntityBean classes A and B which share some common
functionality and state by inheriting from the same superclass S. Then, if S
has some member variable, say a java.util.Vector called "foos", which is
part of the persistent state of both A and B, my question is this: When
should "foos" be initialized?

I am positive that the ejbCreate methods of A and B could include an
instantiation like "foos = new Vector();" but if I use this model, I have to
remember to write this instantiation into every subclass of S, and if S has
many member variables (and many subclasses) that becomes error prone. - Not
to mention that if I add a new member variable to S I have to modify all
subclasses A, B, etc. to make their ejbCreate(...) instantiate this new
variable.

My own suggestion is to define a method in S, lets call it "void
instantiateS()" and have subclasses A, B, etc. invoke this method in their
ejbCreate(...).
I believe this method should *not* be the (no-arg) constructor of S as I
expect this contructor will be called everytime an instance of A or B is
loaded from persistant storage (which is unnecessary).

What are your suggestions?
What do you do when using inheritance in your beans?

Any comments would be appreciated.

Randahl.


Reply via email to