简体   繁体   中英

Polymorphic Lists and relational database

I've got an issue modelling a database to store some Java Objects (classical problem of mapping objects to a relational database). A example beeing far better than a long description, here's the stuff.

List of avaliable classes :
- Class A contains an array of doubles
- Class B contains an array of A
- Class C contains an array of B and an array of A (it can go on like that...)

All the objects implements the same parent (let's say StorableObject). There can be as many combinaisons as possible.

How can I store all theses values, and moreover, how can I create a model using some FK that deletes all its child when a parent object is deleted. I mean, since the childs can be stored in various tables, there's no way I can put a simple FK on the parent's field.

Thanks ;)

You've read too many OO books and not one RDB book. Model the data, as data, first. Ensure all your data requirements (as per your question) are satisfied. Then model your objects after the entities.

There are hundreds of identical questions on SO, by people who have arrived at the exact same obstacle . Different table and column names, but the same identical problem. Fowler brought all of you here.

To examine your data closely and provide a useful answer, we need to see your DDL or class diagram or whatever you have.

Just my opinion, but it seems you may be overgeneralizing.

Because as described, it sounds like two tables. A table of "things", and a cross-reference that links a "thing" to its children. If this is the case, the "ON DELETE CASCADE" clause of the foreign key will do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM