Hi,
I just wanted to ask about the missing Optional flag on @OneToMany, and why it doesn't exist?
Checking here:
http://download.oracle.com/javaee/5/api/javax/persistence/OneToMany.html confirms that it definitely isn't present like on the other tags.
Reading EJB3 In Action, for OneTo
One, 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 OneTo
Many 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
Manyto
Many, 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!
Thanks,
MG