Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Embeddable class cannot have @oneToMany relationship

Ranch Hand
Posts: 1756
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I saw this post from Stack overflow :
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.
Himai Minh
Ranch Hand
Posts: 1756
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!