On Wed, 7 Apr 2004, Keith Rogers wrote:

> It seems to me that if you define a reference in a base class, and then
> have another class that inherits from it, then the reference-descriptor
> elements are not inherited.
> 
> For example, using the example at the end of the mail, if you query and
> retrieve a User, you can use a getUserStatus() method to retrieve the
> correct UserStatus object.
> However, if you query and retrieve a Person, you can get the correct id
> of the user status (using a getUserStatusId() method), but calling
> getUserStatus() will return null. 
> Is this the correct behaviour, or have I made a mistake in the
> configuration? If this is the correct behaviour, how could I work around
> it? I'm sure this could be done using the "mapping each class to a
> distinct table" inheritence strategy, but this would involve modifying
> an existing database, which I am reluctant to do for compatability
> reasons.
> 
> Example - please assume that relevent get/set methods are implemented
> 
> public class User
> {
>     protected int id;
>     protected String username;
>     protected String password;
>     protected int userStatusId;
>     protected UserStatus userStatus;
> }
> 
> public class Person extends User
> {
>     protected String surname;
>     protected String forename;
> }
> 
> public class UserStatus
> {
>     protected int id;
>     protected String name;
> }
> 
> <class-descriptor class="User" table="users">
>     <field-descriptor name="id"             column="user_id"       
> jdbc-type="INTEGER" primarykey="true" autoincrement="true"/>
>     <field-descriptor name="username"       column="username"      
> jdbc-type="VARCHAR"/>
>     <field-descriptor name="password"       column="password"      
> jdbc-type="VARCHAR"/>
>     <field-descriptor name="userStatusId"   column="status"        
> jdbc-type="INTEGER"/>
>     
>     <reference-descriptor name="userLevel"
> class-ref="com.mp.hato.ojb.OJBUserLevel" auto-retrieve="true">
>         <foreignkey field-ref="userLevelId"/>
>     </reference-descriptor>
> </class-descriptor>
> 
> <class-descriptor class="Person" table="people">
>     <field-descriptor name="id"                         column="id"    
>    jdbc-type="INTEGER" primarykey="true"/>
>     <field-descriptor name="surname"                   
> column="surname"                    jdbc-type="VARCHAR"/>
>     <field-descriptor name="forename"                  
> column="forename"                   jdbc-type="VARCHAR"/>
>     
>     <reference-descriptor name="super"
> class-ref="com.mp.hato.ojb.OJBUser" auto-retrieve="true"
> auto-update="true" auto-delete="true">
>         <foreignkey field-ref="id"/>
>     </reference-descriptor>
> </class-descriptor>
> 
> <class-descriptor class="UserStatus" table="user_status">
>     <field-descriptor name="id" column="id" jdbc-type="INTEGER"
> primarykey="true"/>
>     <field-descriptor name="name" column="name" jdbc-type="VARCHAR"/>
> </class-descriptor>


As far as I can see, there are two problems here:

* OJB is only aware of the inheritance hierarchy if you declare it in your
class descriptor, e.g. in your case:

<class-descriptor class="User" table="users">
  <extent-class class-ref="Person"/>
...

(Note that this is the opposite direction to the Java 'extends'). 

* Descriptors are not inherited in OJB. If you want a subclass to have the
same fields/references/collections as its base class in terms of the
database, then you have to specify them again in the class-descriptor of
the subclass. You should also take care that the definitions of the
primary keys are equal, otherwise you might run into problems (in your
case the primary key of User is autoincrement, but the primary key of
Person is not).
This is btw. independent of whether you map the base- and subclass to the
same or to different tables.

You might want to check out the XDoclet OJB module which handles this
automatically for you.

Tom


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to