You could use a 1 to 1 mapping between the 2 beans.  This doesn't
achieve the goal of storing the data in the same table, but has the same
effect.  The user bean would have fields id, username, and password.
The researcher bean would have cmp fields firstname,lastname and
affiliation and cmr field to UserBean.  If you wanted the client to call
ResearcherBeanInstance.getUsername(), you could implement a business
method in your research that just calls UserBeanInstance.getUsername().
The ResearchBean could have the same PK as the UserBean.  The user
methods could be put into an interface and the beans can implement this
interface.

Your code would look something like this

public interface IUser extends Serializable
{
    public abstract Integer getId();
    public abstract void setId(Integer id);
    public abstract String getUsername();
    public abstract void setUsername(String username);
    public abstract String getPassword();
    public abstract void setPassword(String password);
}

public abstract class UserBean implements IUser,EntityBean
{
    // ... EJB callback methods

    // CMP methods defined in IUser
}

public abstract class ResearcherBean implements IUser,EntityBean
{
    // ... EJB callback methods

    // CRM methods
    // either create the userbean in an ejbPostCreate method or
    // accept an existing instance and set it
    public abstract UserBeanLocal getUser();
    public abstract void setUser(UserBeanLocal user);

    // CMP methods
    public abstract Integer getId();
    public abstract void setId(Integer id);
    public abstract String getFirstName();
    public abstract void setFirstName(String firstName);
    public abstract String getLastName();
    public abstract void setLastName(String lastName);
    public abstract String getAffiliation();
    public abstract void setAffiliation(String affiliation);

    // ... Business methods
    public String getUsername()
    {
        return getUser().getUsername();
    }

    public void setUsername(String username)
    {
        getUser().setUsername(username);
    }

    public String getPassword()
    {
        return getUser().getPassword();
    }

    public void setPassword(String password)
    {
        getUser().setPassword(password);
    }
}

James Hicks

-----Original Message-----
From: A mailing list for Enterprise JavaBeans development
[mailto:[EMAIL PROTECTED] On Behalf Of Jason W. Solinsky -
Silver Bullet Software
Sent: Tuesday, September 02, 2003 5:48 PM
To: [EMAIL PROTECTED]
Subject: A Scheme for CMP Entity Inheritance: Is it allowed under the
standard? Is it a good idea?


Some (perhaps all?) major J2EE servers allow a single CMP entity bean to
obtain its data from multiple tables. I wish to use this feature to
implement inheritance, and I would like to know if it is:

A) Allowed and appropriate under the existing J2EE standards and
B) A good idea


Suppose I have an entity bean called User.
It has the following fields:

Integer id;
String username;
String password;
String type;

and is mapped to a table called Users.

I want to create a subclass of User called Researcher. I want the entity
bean to have the following fields:

Integer id;
String username;
String password;
String type;
String firstName;
String lastName;
String affiliation;

I want to map the first four fields to the Users table and the last
three fields to a Researchers table which would also have an id field
for the purpose of joining the two together.

Now I have two beans, one of which is logically a subclass of the other
but which have no formal relationship as far as the EJB container goes,
sharing the same database columns. When I want to manipulate all users I
can use the User bean. When I want to deal with just Researchers, I can
use the Researcher Bean. I can use the type field to inform me which
users are also researchers if I ever need to know this.

Is this legal and/or appropriate as far as the specs are concerned? Is
it a good idea? If not, what other techniques are people using?

Thanks,

JWS

========================================================================
===
To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body of the message "signoff EJB-INTEREST".  For general help, send
email to [EMAIL PROTECTED] and include in the body of the message
"help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to