Firstly...ignore Gavin. :D I think he sometimes forgets that this is the BEGINNERS forum and sometimes comes across a little aggro.
<cowers from Gavin>
Secondly. I somewhat disagree with Gavin. (a first)
I never once use child.setParent() anywhere in my code for 1-to-1's.
I only ever set the parent in my (1-to-M) collections (as I iterate them), never in my 1-to-1's.
At first I was thinking that Gavin was right completely right, as I use JAXB to unmarshal an XML file, and that creates the structure for me. However, I have recently started generating XML files, and I checked there, I have this:
Code:
outXML.setMatter(matter);
And nowhere do I do a
Code:
matter.setXML(outXML);
I simply create a new instance of Matter, and do sets of all the normal properties, then assign it to the XML.
That said, I found that for all relationships, when marshalling with JAXB (I posted this elsewhere), it is a good idea to do:
Code:
matter.setXML(null);
to break an infinite loop when writing to file.
Anyway, in the end, for 1-to-1's, although you don't actually have to set the child.setParent() when processing, you actually do, as Gavin kind of said, have to have the properties in both objects for Hibernate to work properly. :)
Summary:
--------------------------------------------------------------------------------------
Yes, you do need the get/set in both, and yes it can look messy, but if you code properly, you yourself only have to actually do one of the sets. It's all part of the the whole O/R Mapping world.
--------------------------------------------------------------------------------------
-G