• 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Embeddable class cannot have @oneToMany relationship  RSS feed

 
Ranch Hand
Posts: 1703
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: 1703
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.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!