The answer to that question is that you don't. Think of NHibernate as a synchronization tool, not a database persistence tool. It's trying to make it so that the database is a form of "extended memory". You want both the Person assigned to the Car and the Car added to the person's collection when you load it from the database right? Therefore that's what it should look like before you save it as well.
This is a far superior way to handle it in your code because now you don't have to worry about if you are dealing with a person/car combination that has been persisted and loaded already or not. Your code knows that the Person should always be assigned and the collection of cars should match. Writing methods to add the car to such as Person.AddCar(Car car) is a simple pattern to follow which I believe most of us use. There used to be tools that would do this for you, but I found that confusing and error prone. On Mon, Mar 9, 2009 at 12:39 PM, graphicsxp <[email protected]>wrote: > > Thanks Jay, > > So basically you are saying that what I do is correct, but your > solution is a shortcut as you are assigning the Person entity to the > Car entity within the Add() method. > > Fair enough, but my initial question was, how can I do it without > having to specify the Person entity, i.e have the attribute > inverse=false without raising an exception.... > > On 9 mar, 16:37, graphicsxp <[email protected]> wrote: > > ok i got it you mean adding the Car entity to my collection of cars in > > Person. :) > > > > But that's exactly what I do already : > > > > myPerson.cars.add(new Car(1, myPerson)) > > myPerson.cars.add(new Car(2, myPerson)) > > > > sorry I should have included this code in my first post. > > > > Yet I still get the problem..... > > > > On 9 mar, 16:22, graphicsxp <[email protected]> wrote: > > > > > Hi Roger, > > > can you explain ? what's in AddCar ? > > > > > On 9 mar, 15:56, Roger Kratz <[email protected]> wrote: > > > > > > myPerson.AddCar(myCar) > > > > > > -----Original Message----- > > > > From: [email protected] [mailto:[email protected]] On > Behalf Of graphicsxp > > > > Sent: den 9 mars 2009 16:52 > > > > To: nhusers > > > > Subject: [nhusers] issue with bi-directional one-to-many association > when saving > > > > > > Hello, > > > > > > I have a question about bidirectional one-to-many associations. Let's > > > > consider we have a class Person which can hold a collection of > > > > entities of type Car. > > > > > > My mapping file for Car would have something like : > > > > > > <many-to-one name="PersonID" cascade="none" column="PersonID" not- > > > > null="true" /> > > > > > > and my mapping file for Person would contain : > > > > > > <bag name="Cars" inverse="true" lazy="true" cascade="save-update"> > > > > <key column="PersonID" /> > > > > <one-to-many class="Car" /> > > > > </bag> > > > > > > Now that implies that anytime I want to create a Car object I also > > > > need to do : > > > > > > myPerson = new Person() > > > > myCar.person = myPerson > > > > > > This is tedious and I don't want to have to do that. The problem is > > > > that if I set the attribute inverse to false, then when I try to save > > > > a Person, I get an exception because all the Car entities I've added > > > > to this Person have a null CarID. If I set CarID to be > not-null=false, > > > > then there is no exception but it inserts null into my database. > > > > > > How can I have NHibernate to automatically set the PersonID field of > > > > each Car objects in my collection to the PersonID of the Person I > just > > > > saved ? > > > > > > Hope this is clear enough > > > > Thanks > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
