0

I need to suggest three variants of the structure of the database that reflects object model of classes. The structure of the database should allow understand class inheritance. For simplicity, classes contains only attributes (no methods).

I consider Single Table Inheritance, Class Table Inheritance, Concrete Table Inheritance patterns.

But not everything is clear for me. The question are:

1) Where are classes stored in database, and where are instances of classes stored?

2) Let's say, class B extends class A. How to make instance b of the B understand that it is a derived A? How to make b understand that this is an instance of class B, not class A?

3) Is there a patter in which an addition of a new class to inheritance hierarchy does not require changing the structure of tables?

4) Is there a pattern which allows multiple inheritance?

UPD: Probably I expressed my thoughts not clear enough. Let's say it is not java, but any another language. This quiesion is in context of database structure. Is there pattern (like above) which allows multiple inheritance?

Thanks.

artur me
  • 111
  • 1
  • 9

2 Answers2

2

1) Where are classes stored in database, and where are instances of classes stored?

Classes are not stored in the database, only instances. In most cases/mappers a discriminator tells the mapper the type/class of an instance.

2) Let's say, class B extends class A. How to make instance b of the B understand that it is a derived A? How to make b understand that this is an instance of class B, not class A?

Again this is normally done via discriminator columns.

As for "How to make instance b of the B understand that it is a derived A": you do that in the code, i.e. B extends A. If you change that later you might break existing data so you'd either need to think about inheritance when designing the model or have some migration strategy when you need to make changes.

3) Is there a patter in which an addition of a new class to inheritance hierarchy does not require changing the structure of tables?

This could be achieved with table-per-class or joined strategy. In both cases you'd need to add tables for new subclasses but you'd not have to alter the existing ones (provided you already have a discriminator column, otherwise you'd have to add that once).

4) Is there a patter which allows multiple inheritance?

No, Java doesn't allow multiple inheritance (this applies to orm as well) and it's not needed in most cases anyways. Try composition instead.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • 1) But as I understood from [here](http://stackoverflow.com/questions/657803/where-are-java-classes-stored-in-oracle), there are tables storing java byte code and sources. Could you plase shed light on this? I don't get it. 4) In context of database (not java) what if multiple inheritance is used, should I use multiple discriminator columns? – artur me Oct 09 '15 at 09:05
  • @a.yuzhanin The link you posted for 1) refers to the Oracle database and from reading it it is not really clear what that bytecode and source is used for. It might be some sort of stored procedure etc. but it surely doesn't apply to general ORM etc. As for 2): you'd probably still have one discriminator since it describes the class a row should be mapped to. Inheritance information is normally stored in the code itself. Besides that multiple inheritance is often not the wisest approach (which is why Java doesn't support it). – Thomas Oct 09 '15 at 10:09
0

1) I don't know

2) In a if statement you can ask if it is and instance of an object like this:

if ( obj instanceof A ) {
    obj.method1();
} else {
    obj.method2();
}

3) That is possible when the class is not 'abstract'. And when there are no abstract methods.

4) You can not use more inheritance then one. But you can implement more interfaces the one. Maybe this is the way for you. Otherwise you have to use a inheritance of more classes. So you get a sub-, sub-, sub- class or something.

R. Suntjens
  • 121
  • 6