I'm creating a system where I have some entities that have some properties that are common, like address (with street, number, zip etc.) and phone (number, type, etc.) and I don't want to repeat these columns on each entity.
Here' a example:
- Student has address and phone
- Teacher has multiple addresses (home and office) and multiple phones (home, mobile, office)
- StaffMember has address and multiple phones (home, mobile and office)
I've used something like that while developing Ruby On Rails using polymorphic associations. I've searched for something like on Java/JPA/Hibernate and couldn't find something much like it. I've found many things about JPA inheritance but I don't quite understand it.
Can you give me a example on how to model it and how to use it?
EDIT
After reading my question I think it's not clear enough, so let me add here the database schema I have:
Student
-------
id bigint
name varchar
birth_date date
...
Teacher
-------
id bigint
name varchar
birth_date date
department varchar
...
StaffMember
-------
id bigint
name varchar
birth_date date
department varchar
function varchar
...
Address
-------
id bigint
street varchar
number int
...
entity_id bigint
entity_type varchar
Phone
-----
id bigint
type varchar
number varchar
...
entity_id bigint
entity_type varchar
And for both Address and Phone the columns entity_id and entity_type are references to Student, Teacher and StaffMember.
But how to map it using Hibernate/JPA?
spring.jpa.generate-ddl=trueand let hibernate generate the schema for you