| Author |
The "optional" element on @OneToMany and @ManyToMany
|
Treimin Clark
Ranch Hand
Joined: Nov 12, 2008
Posts: 757
|
|
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
Joined: Nov 24, 2005
Posts: 14672
|
|
@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.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Treimin Clark
Ranch Hand
Joined: Nov 12, 2008
Posts: 757
|
|
|
Got it, thanks Christophe.
|
 |
Amandeep Singh
Ranch Hand
Joined: Jul 17, 2008
Posts: 837
|
|
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.
|
SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
|
 |
Raf Szczypiorski
Ranch Hand
Joined: Aug 21, 2008
Posts: 383
|
|
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.
|
 |
 |
|
|
subject: The "optional" element on @OneToMany and @ManyToMany
|
|
|