I have several classes in my application:
- Individual, mapped to the Individual table
- Company, mapped to the Company table
- Address, mapped to the Address table
I want to build on-to-may associations between Individual and Address, and also between Company and Address, since naturally both individuals and companies can have zero or more addresses. I also want to use the same columns in the Address table to represent this association in the DB - one column referring to the ID of the owner entity, another signifying its type. So in SQL I would do something like this to extract addresses of the individual with id 5:
select * from Address where ref_id = 5 and ref_type = 'Individual'
For those that wonder why I do it, my motivation is twofold:
- Consolidate tables/columns that use similar data.
- Use more generic schema to provide future expansion of the domain model without requiring DDLs.
So the question is: how do I define such association in Hibernate? I'm new in Hibernate and I couldn't find the way to do it after several hours of struggling.
Thanks.