File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes No Optional on @OneToMany Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "No Optional on @OneToMany" Watch "No Optional on @OneToMany" New topic

No Optional on @OneToMany

Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226

I just wanted to ask about the missing Optional flag on @OneToMany, and why it doesn't exist?
Checking here: confirms that it definitely isn't present like on the other tags.

Reading EJB3 In Action, for OneToOne, they give the example of BillingInfo to a User.
A User may have 1 instance of BillingInfo, however they don't have to.
BillingInfo must have a User, or there's no point storing it.
So, on the User class, @OneToOne is used and on the BillingInfo class "optional=false" is used. Pg. 244, "This means that a BillingInfo object cannot exist without a related User object".
It also means, that the user's @OnetoOne has a defaulted optional flag of true, so a user can exist without BillingInfo.
Ok - happy.

Moving on to OneToMany they use the example of an Item and Bids.
An Item may have many bids.
Bids must related to an item or they make no sense.
So, on the Item class, a collection of Bids is used, and we're told that @ManyToOne's are always the owner (makes sense from a db point of view), so Item has the mappedBy element.
However, there is no way to set the optional flag, and I don't know why that is.
I would have suggested that if optional was 'false', it would mean the relationship wasn't optional, therefore if a item exists, it must have bids.
and I would have suggested that a true value would say that an Item can exist without bids.

Skipping forward to ManytoMany, it seems as if when you are referencing a collection, the optional flag disappears. I wonder if it is because you always have the relationship, but you just end up with an empty collection if (for example) there are no bids on an item. The relationship is always optional when referencing a collection?

Hope that makes sense - it's quite difficult to get your head around!



28/06/06 - SCJP - 69%, 05/06/07 - SCWCD - 92%, 28/02/08 - IBM DB2 v9 Fundamentals (Exam 730) - 87%, 18/11/08 - IBM DB2 v9 DBA (Exam 731) - 89%, 26/02/11 - SCBCD - 88%
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper

Joined: Apr 07, 2010
Posts: 2106

Hi Mark,

There are actually two relationships that have the optional flag:
  • @OneToOne unidirectional
  • @ManyToOne unidirectional

  • Why there is an optional flag in these interfaces and no optional flag in the other interfaces is not described, so we can only try to figure out what the writers meant when they were writing the specs.

    I think that the reason might be related to the fact that the relations are unidirectional. With an unidirectional relation it can be that an Entity can only exist when the other Entity is there. With a bidirectional interface both entities have a right to exist and at some point in time they can be related to each other.
    Unidirectional: a Phone without an Owner doesn't make sense (or in other words: we aren't interested in the phones number if it is not related to a person, and we don't want to store it in the database unless the relationship is there)
    Bidirectional: A Person can have an Address, an Address can have a Person, but it is also possible to have a Person without an Address and an Address can be without a Person at some point in time. So it makes sense to always allow null values in the database for the relationship attributes in this case.

    Does that make sense?


    praveen Ji

    Joined: Nov 03, 2009
    Posts: 12
    Even I reached to this thread looking for the answer of the question: why no "Optional" on @OneToMany annotation.
    Since I did not find answer by quick googling.. I gave it some more thought. Though the thread is a bit old but still thought to share my view for other's benefit.
    My understanding is as below:

    In associations like one-to-many and many-to-many, the many side entity(referenced entity) is declared as a Collection<otherSideEntity>. In case there does not exist any related instances at runtime, the collection will be empty but NOT null. Hence having Optional element for such association is not needed.
    Whereas in associations like one-to-one, many-to-one, the other side(referenced entity) is a single-valued reference. So you do have a possibility of NULL value for referred entity, if the association is optional. and hence the optional element.
    Jayesh A Lalwani
    Saloon Keeper

    Joined: Jan 17, 2008
    Posts: 2749

    Right,, OneTOMany relationship implies that the parent entity can have 0..n children. There is no need for optional because mathematically speaking, OneToZero is a a special case of OneToMany
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Saloon Keeper

    Joined: Apr 07, 2010
    Posts: 2106

    Thanks for explaining how it works!

    I agree. Here's the link:
    subject: No Optional on @OneToMany
    It's not a secret anymore!