Are inheritance derived classes different tables in Mysql?

Asked

Viewed 84 times

1

If I use inheritance and have 4 derived classes and my parent class is abstract, these 4 derived classes will each have a different table in the Mysql database?

1 answer

1


It is not so simple to transpose the object-oriented model of programming languages into the relational database.

Not even in Dbs that have inheritance like Postgresql. In fact, don’t waste time with it if you use it, it’s not the mechanism that people expect.

I often say that OOP is good for mechanisms, not business rules. People ignore it and then get into trouble.

There’s even a fear that takes care of it: impedance Ismatch between OO model and relational. It’s something well debated and they say an ORM solves it. It even does, if you know what you’re doing and if it’s the right thing to do, but it also brings problems. That’s why I say: either you use a 100% object-oriented database (there are no established options); or you make the relational model in your application (in the business rule part), which is what "everyone" recommends doing nowadays anyway (but in general most people do not understand). Keep in mind that almost every inheritance that people make in business rules is wrong.

And then we come to the problem of context. We don’t know what heritage you’re talking about, where it’s specifically being applied. Each case can be a solution, and the most common will be to eliminate the inheritance.

For example, if you have a derived table called Cliente that comes from Pessoa is probably wrong. I I talk about it in several answers here at Sopt.

If you have a PessoaJuridica and PessoaFisica that derives from Pessoa depends. It may be that you should only have one table Pessoa which ends up accepting data from derivatives. Conceptually it may not be ideal, but in practice it may make more sense. Or it can actually have two tables the data of each one totally separate. In fact the most correct is always to have tables only for concrete classes, and all concrete classes should have a table for it.

But it doesn’t always work well. In some cases it may be until inheritance is the problem and should eliminate it. The right thing isn’t always the best thing to do.

If every idea is wrong it could be that you’re just wondering how to get around the problem instead of solving its root.

I would make a table for each concrete class, but so may just be perpetuating the existing problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.