File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The "optional" element on @OneToMany and @ManyToMany

 
Treimin Clark
Ranch Hand
Posts: 757
  • 0
  • Mark post as helpful
  • send pies
  • 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).

 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, thanks Christophe.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic