Hi, I saw this post from Stack overflow :https://stackoverflow.com/questions/18848294/how-to-define-a-elementcollection-of-an-embedded-type-which-contains-onetomany
It concludes that embeddable class cannot have @OneToMany relationship as specified by the specification.
The explanation from someone is :
Embeddables don't have their own identity, so they cannot be uniquely identified. 1:M and M:M require the target side knowing what object they point to - a back reference is stored in the database. But since nothing uniquely identifies your ContractInformation objects, they can't be distinguished from one another, so there is no way to tell which ContractInformation a particular Phone belongs to.
But I think that may not be the reason. I think the mapping cannot distinguish the embeddable class, ContactInformation, but it can distinguish the Phone entities encapsulated in the ContactInformation object.
I guess the real reason is that embeddable class can be reused in different types of entities. We may define a unidirectional @OneToMany relationship inside an embeddable class, which will be reused in different entities.
Some owing entities may need this @OneToMany relationship, but some don't.
With a @OneToMany relationship, the mapping will create a join table.
So, if the owning entities do not need this @OneToMany relationship, we will have redundant join tables.
If you know the real reason why @OneToMany or @ManyToMany cannot be in an embeddable class, please let us know.Thanks.
posted 1 year ago
Let me answer my own question. I tried with chapter 10's overridingEmbeddedRelationship example,
To my surprise, a join table called Customer_phone is created in the DB with two columns Customer_ID, otherphones_NUM.
So, the EclipseLink allows @oneToMany relationship or @ManyToMany relationship to be included in embeddable class even though the spec says embeddable class can only have many-to-one or one-to-one relationship for the owning entities.
Let me tell you a story about a man named Jed. He made this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java