Hi Thorsten, Matthias, On Monday 08 December 2003 23:20, Matthias Bohlen wrote: > Hi Thorsten, > > > either a cat or a dog. How can I get out of it? > > Well :-) you simply won't get out of it with EJBs. They are simply not > designed for true inheritance. > > > But why is the ejb inheritance support in the new version if it makes > > no sense? > > > Isn't it useful in some cases even it is not the pure object oriented > > design? > > Richard implemented inheritance purely on the Java level. One bean > inherits from another and aggregates all its superclass attributes into > the same database table (Richard, is that correct?).
Not quite. It inherits the attributes from the super class, but they are stored in the database table corresponding to the concrete bean. For the cat-dog-animal example, all of the CAT, DOG and ANIMAL (if AnimalBean's not an abstract super class but a concrete bean as well) will have a NAME column storing the name of the pet. Storing the attributes in the super class table isn't easily possible with CMP. The only way I can think of is a hidden CMR relation to the parent, and delegating the inherited attributes to the parent instance, but this would make it impossible to use the inherited attributes in an EJB query. Implementing inheritance is possible with BMP, but if you're going to implement the persistence by hand anyway, you can just as well use a different persistence layer and not have to reinvent that particular wheel... > It is just a shortcut to simplify programming a little. It's a little bit more than that, actually. A CatBean and a DogBean that derive from AnimalBean *are* instances of AnimalBean for all intents and purposes. They implement the common entity interface, have all the attributes (and business methods, for that matter) of AnimalBean and so on. The only thing that is not inherited (and can't really be with CMP beans) is finder methods and container managed relations. It's not possible with EJB semantics to have a finder on AnimalBean that returns both cats and dogs, and it is not possible to have a container managed relation to an AnimalBean that relates to anything other than "generic" animals. > OK, so I'd say: do not try to make inheritance REALLY work with EJBs. > You'll spend about 6 months and end up with a very ugly solution. Use an > O/R mapper and you'll be happier! :-) I'll second that. If you need real support for inheritance, ditch EJB entity beans and go with a different persistence layer. > P.S.: Richard, how about using brute force and encoding the type of the > subclass in the primary key of the entity? Sure - I'll go work on it as soon as you tell me how to implement "select OBJECT(o) from MyDerivedBean o" so that it won't find the data for other classes that are persisted in the same table ;-) Bye, Richard -- Richard Kunze [ t]ivano Software, Bahnhofstr. 18, 63263 Neu-Isenburg Tel.: +49 6102 80 99 07 - 0, Fax.: +49 6102 80 99 07 - 1 http://www.tivano.de
pgp00000.pgp
Description: signature
