This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Embeddable class cannot have @oneToMany relationship  RSS feed

 
Ranch Hand
Posts: 1701
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Himai Minh
Ranch Hand
Posts: 1701
12
  • 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.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!