On Wed, 17 Jul 2002, Vikram Naik <[EMAIL PROTECTED]> wrote:
>Hello All,
>
>1. There is one pre-defined Ejb (entity bean) User.
>2. Now for inheriting this EJB say NewBean extends User, will I have to
>extend its remote & home interfaces also.
Well, it entirely depends on what sort of behavior you're looking
for. EJBs are regular Java Classes, so the Java classes that make
up your EJBs can inherit, or not inherit at your choosing. However,
the App Servers underlying support for the EJBs won't understand
inheritance and won't provide value added support for it.
What does this mean? Well consider the following example:
Let's say you have 3 different entity beans, Pet, Dog, and Cat,
with the idea being that Pet is a base class of both Dog and Cat.
For Pet, you'd have:
The Local Interface-- Pet (extends EJBLocalObject)
The Home Interface-- PetHome (extends EJBLocalHome)
The Bean Class-- PetBean (implements EntityBean)
Similarly, for Cat, you'd have:
The Local Interface-- Cat (extends EJBLocalObject)
The Home Interface-- CatHome (extends EJBLocalHome)
The Bean Class-- CatBean (implements EntityBean)
And for Dog:
The Local Interface-- Dog (extends EJBLocalObject)
The Home Interface-- DogHome (extends EJBLocalHome)
The Bean Class-- DogBean (implements EntityBean)
Now, let's say Dog's are going to have the same methods as other
Pet's, but with a few extra methods attached, for example getBreed().
You could, at your option have the Dog interface extend the Pet
interface. You could even, if so desired to save a few lines of
code, have the DogBean extend the PetBean. And you could do the
same for Cats. You could even have the home interfaces for Cat and
Dog extend PetHome if it makes sense for the CatHome and DogHome
to have the same methods as PetHome does. This should all compile
and deploy just fine.
Given this, I could even do:
DogHome dogHome = (DogHome) naming.lookup("DogHome");
Dog myDog = dogHome.findByName("Fido");
Pet myPet = myDog;
//Works because the Java interface Dog does indeed extend Pet.
So far, so good right?
Well, here's where the idea of inheritance breaks down... Let's say
I attempted to find my Dog Fido using the PetHome instead.
PetHome petHome = (PetHome) naming.lookup("PetHome");
Pet myPet = petHome.findByName("fido");
The object that gets returned will be neither a Dog nor a Cat,
it will only be of type Pet. If I attempt a cast...
Dog myDog = (Dog) myPet;
This will throw a ClassCastException every time. As far as the EJB
world is concerned, the PetHome interface returns objects that are
ONLY Pets and NEVER Dogs nor Cats. You need to go to DogHome to
get a Dog and to CatHome to get a Cat. Just because you think your
Dog is also a Pet doesn't mean that PetHome will get it for you.
As far as the EJB world is concerned, Pets, Dogs, and Cats are
three completely different types of Entity Beans and there's no
relationship between them.
So in short, your Java classes can inherit, but that doesn't mean
the app server will consider the EJBs themselves to be related or
inherited from each other.
Doug
P.S. Typically what you'd do for the scenerio I just described
is just have the Pet interface, and provide neither a PetHome
nor a PetBean. This way it's clear that there are only Dog and
Cat EJBs, each coming from their own home interface. And yet,
your helper classes can still have methods that work with Pets
in general. For example:
public class PetActivitiesBean implements SessionBean
{
//You can pass in either a dog or a cat to play with.
void play(Pet pet);
}
Anyway, I hope this helps clear things up.
===========================================================================
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".