Today I”ve just wasted a couple of hours trying to get something to make NHibernate persist an custom object graph. My scenario wasn”t really complicated and looked something like this:
root object —>Item(s)—->Components
My root is a reference object which has a collection of other reference types (Item). Now, the fun part: I was under the impression that adding a bag to my root object and putting a <one-to-many> element inside it was enough for persisting the root and the associated collection. oh well, not so. If you try to do that you”ll have to configure your FK so that it accepts nulls (and in this case, it will stop being a FK, right?). It seems like the easiest way is to have a bi-directional association. When you do this, NH will automatically persist the object graph correctly (many thanks to marcal for confirming this on the NH forums).
Ok, after solving this, then another problem popped up: after running some tests, I”ve noticed that the component collection owned by the item object wasn”t being persisted. WTF? why? After asking in the forums, I”ve ended up recalling an old post that talked about some problems regarding WCF and collection serialization. And so I decided to try it out and guess what? it worked. So, if you want to pass collections through WCF services, you should take a look at this post.