Hibernate: mapping two classes to a common third class
I am searching for the correct mapping of a many-to-one relationship of two classes pointing on a third one. An example is shown in the image below:
In words; a Person has 1..n addresses a Company has 1..n addresses
I'd like to have a single table for all addresses. For the moment a uni-directional relation is开发者_如何转开发 all I need, but it would be also nice to see how this works with bi-directional relations.
I believe the correct database representation is like in the next image
But I don't know how to translate this into a hibernat xml file or a java class using hibernate annotations.
This problem has been discussed 5 years ago in the hibernate forum: "https://forum.hibernate.org/viewtopic.php?t=961387" but I think they found no satisfactory solution.
regards.
Julien
That's a very normal "unidirectional one-to-many with join table" relationship as described in Chapter 8 of the Hibernate reference and Chapter 2 of the Annotations reference.
精彩评论