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
-~----------~----~----~----~------~----~------~--~---

Reply via email to