Polymorphic Lists and Relational Database

I have a problem modeling a database to store some Java objects (the classic problem of mapping objects to a relational database). An example would be much better than a long description, that’s what.

List of available classes:
- Class A contains an array of doubles
- Class B contains an array A - Class C contains an array of B and an array A (it can go the same way)

All objects implement the same parent element (say, StorableObject). There may be as many combinations as possible.

How can I save all these values, and, in addition, how can I create a model using some FK, which removes all its children when the parent is deleted. I mean, since children can be stored in different tables, I cannot put a simple FK in the parent field.

Thanks;)

+4
source share
2 answers

You read too many OO books, not just one RDB book. First model the data as data. Make sure that all your data requirements (as per your question) are met. Then model the objects after the objects.

There are hundreds of identical questions about SO people who have reached the same hurdle . Different table and column names, but the same problem. Fowler brought all of you here.

In order to carefully examine your data and provide a useful answer, we need to see a DDL diagram or class or something else that you have.

+5
source

Just my opinion, but it looks like you might be too generalizing.

Because, as described, this sounds like two tables. A table of β€œthings” and a cross-reference that links the β€œthing” to her children. If so, the "ON DELETE CASCADE" clause of the foreign key will do the trick.

+3
source

Source: https://habr.com/ru/post/1337927/


All Articles