• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The "optional" element on @OneToMany and @ManyToMany

 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why @OneToMany and @ManyToMany annotations doesn't have the "optional" element? Any explainable reason?

(This element is available on @OneToOne and and @ManyToOne only).

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OneToMany (1..*) and @ManyToMany (*..*) shows that there may be any number of entities on the "right" side of the relation. There may be 0, 1, 2... The "right" side is represented by a collection.

@OneToOne(1..1) and @ManyToOne (*..1) usually represent a tight relation between one or many entities to a single entity. There may be time when you'd like to say that there's only one entity, but it's not always there. You can use "optional" to tell the container that the relation you want is something like (1..0 or 1) or (*..0 or 1).

That's why "optional" only makes sense for @OneToOne and @ManyToOne.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, thanks Christophe.
 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:@OneToMany (1..*) and @ManyToMany (*..*) shows that there may be any number of entities on the "right" side of the relation. There may be 0, 1, 2... The "right" side is represented by a collection.

@OneToOne(1..1) and @ManyToOne (*..1) usually represent a tight relation between one or many entities to a single entity. There may be time when you'd like to say that there's only one entity, but it's not always there. You can use "optional" to tell the container that the relation you want is something like (1..0 or 1) or (*..0 or 1).

That's why "optional" only makes sense for @OneToOne and @ManyToOne.



but again here, i can justify here

@OneToOne(1..0), will be a unidirectional relationship.. optional = true

@OneToOne(1...1), will be a bidirectional relationship. optional = false

but I think there is a possibilty of @OneToMany as unidirectional or bidirectional relationship...
in that case, it might require's optional element.

Correct me if i am wrong.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see it this way - the many end is a collection, and it is always optional (empty collection). Whereas you can make the 'one' end to be required (by forcing a not-null FK referencing PK), I see no way to force the many end be required - the many end in OneToMany is the owner, so it is that end that keeps the FK, which brings us to the start - it may be made required.
Also, consider bidirectional OneToMany / ManyToOne(optional = false) - when ManyToOne is required, you know for sure that there is at least one element on the 'many' side for each entity in the 'one' side, don't you?
Cheers.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic