[odb-users] Inheritance

Boris Kolpackov boris at codesynthesis.com
Fri Jul 8 04:52:01 EDT 2011


Hi Jan,

Jan Kuentzer <kuentzer at googlemail.com> writes:

> I am facing some problems concerning inheritance and hope that I only missed
> something. We have some data structure like the following:
> 
> class employee
> {...}
> 
> class permanent_employee: public employee
> {...}
> 
>  class temporary_employee: public employee
> {...}
> 
> class company
> {
>     std::vector<employee*> employees;
> }
> 
> If we generate with ODB the sql each class get it's own table and the vector
> is associated with employee ids. But the stored objects are sometimes
> instances of permanent_employee, temporary_employee or employee.
> Therefore the object ids are not correct in the database since for queries
> ODB always searches for the corresponding entries in employee, where they
> cannot be found (it is stored in the derived class tables)
> Did I understand something wrong and there is a workaround for that? Or is
> this what you meant with chapter 8.2 which will come in the future?

Yes, this is an example of polymorphic inheritance which is still on
the TODO list. There are generally three different approaches to
modeling polymorphic inheritance in the database:

1. Table per hierarchy, where all derived objects belonging to a hierarchy
   are stored in a single table.

2. Table per difference, where additional columns corresponding to the
   derived classes are stored in additional tables.

3. Table per class, where each class in a hierarchy gets its own
   independent table that contains all its members.

The first approach is the most inflexible: you have to provide the so-
called discriminator (basically a value that distinguishes different
classes) and the whole hierarchy must reside in a single header. But
it is also the fastest.

The second and third approaches are more flexible but are generally
not as efficient as the first one.

Do you have any preference as to which approach would work best in
your case?

There is also a way to emulate polymorphic inheritance for the time
being that will work well with load() and object relationships but
won't handle queries. Let me know if you would be interested in this
and I will elaborate.

Boris



More information about the odb-users mailing list