Hibernate - entity design with multiple dependencies I am currently working on an API and came across the following design.



    // not mandatory
    @ManyToOne
    @JoinColumn(name = "person_id")
    private Person person;


Actually, a telephone number should belong to a company, person or a contact form. However, this is logically not possible with the current design. So there is a small validation at the service level to make sure that there is always at least one dependency. 

Now I was wondering if there is a better way to solve this. Because theoretically something can always go wrong and dependencies can be lost and the data would still be valid according to today's status.

I thought about creating a separate phone number object for each entity that inherits from the phone number. But somehow I don't really like the solution, but I think it's better than the previous one. What I don't like so much is that a lot more objects are created which also contain inheritance. 



Is the new approach ok or how do you solve such problems?
I don't see anything wrong with your approach. 

Another option is to add a PhoneNumberType to the PhoneNumber table.

In any case you should ensure the table has appropriate constraints added so that a PhoneNumber must relate to a table.