In my practice, there are often problems mapping an OOP approach to relational databases in the case of fields of the same type in the same class. I can not understand how to solve this problem in the most effective way.
More detailed example:
I have a Person entity that has the address and workAddress . Moreover, both subentities have the same set of fields.
In code, it is something like this (pseudocode)
class Person Address address Address workAddress How to implement this using programming languages ​​is understandable. The question arises how to do this in the database. I can do, for example, like this (pseudo sql ):
CREATE TABLE address (id SERIAL PRIMARY KEY); CREATE TABLE PERSON ( id SERIAL PRIMARY KEY, address FOREIGN KEY (address.id), workdAddress FOREIGN KEY (address.id) ); This will work, but this is not entirely correct from the point of view of the DB theory. After all, we will have one-to-many from the address-person. Even twice.
You can still go this way:
CREATE TABLE PERSON ( id SERIAL PRIMARY KEY ); CREATE TABLE address_type ( id SERIAL PRIMARY KEY, name VARCHAR(50) ); CREATE TABLE address ( id SERIAL PRIMARY KEY, person_id FORIGN KEY(person.id), address_type FORIGN KEY (address_type.id) ); Here we get the right solution from the point of view of the relational model, but from a very bad solution for OOP. Of the minuses here: an additional JOIN , an additional table with types, it is inconvenient to work with this in the code (you need to go through all the addresses to find the right one or supplement the selets).
I was looking for a solution in the most popular ORM - Hibernate . I did not find there anything to solve such a problem (maybe I looked badly).
Are there any common practices, tools or mechanisms to solve such problems?