Thanks for the hints, Diego.  I have the reference from Vehicle to Person as
"public Person BelongsTo { get; set; }", and the mappings as you suggested.
So I dug a little more and realized that when I do this:

    session.Save(new Person() {
        Name = "Bob",
        Vehicles = new List<Vehicle>() {
            new Vehicle() { Name = "Toyota" },
            new Vehicle() { Name = "Porche" }
        }
    });

the Vehicles are saved (which I thought they weren't), but without the
reference to Person being populated.  Therefore my FindAllPeople function
made it look like the vehicles weren't getting saved.  It turns out that I
have to explicitly set the BelongsTo reference in order to get them to save
properly, like this:

    var person = new Person() {
        Name = "Bob"
    });

    person.Vehicles = new List<Vehicle>() {
        new Vehicle() { Name = "Toyota", BelongsTo = person },
        new Vehicle() { Name = "Porche", BelongsTo = person }
    }

    session.Save(person);

Is there anyway to avoid the explicit back-reference?

Thanks,
-Eric.




On Fri, Jan 22, 2010 at 10:19 PM,
<[email protected]<nhusers%[email protected]>
> wrote:

>   Today's Topic Summary
>
> Group: http://groups.google.com/group/nhusers/topics
>
>    - Cascade save on child collections...<#1265930b4e533921_group_thread_0>[2 
> Updates]
>
>   Topic: Cascade save on child 
> collections...<http://groups.google.com/group/nhusers/t/b422d6caffb52d13>
>
>    "Eric J. Peters" <[email protected]> Jan 22 04:57PM -0800 
> ^<#1265930b4e533921_digest_top>
>
>    Hi-
>
>    I'm sure this is a totally rookie question, but I'm not finding the
>    answer anywhere.
>
>    I have an entity with a child collection of other entites, something
>    like this:
>
>    public class Person
>    {
>    int? Id { get; set; }
>    string Name { get; set; }
>    IList<Vehicle> Vehicles { get; set; }
>    }
>
>    public class Vehicle
>    {
>    int? Id { get; set; }
>    string Name { get; set; }
>    }
>
>    Both sides are mapped (using Fluent NHibernate) as a Reference/HasMany
>    relationship.
>
>    The problem is that when I create an new Person with some new
>    Vehicles, the save isn't cascading to the Vehicles. If I save the
>    vehicles first, then apply them to the new Person, it works fine, but
>    I really want to avoid the extra step of explicitly creating/saving
>    the Vehicles first.
>
>    It seems that this should be something easily achievable with
>    NHibernate, but I'm not finding the magic handshake. Can anybody
>    provide me some advice on how to make these objects work as I'm
>    hoping?
>
>    Thanks!
>    -Eric.
>
>
>
>
>    Diego Mijelshon <[email protected]> Jan 22 11:53PM -0300 
> ^<#1265930b4e533921_digest_top>
>
>    You say that both sides are mapped but I don't see the reference from
>    Vehile
>    to Person.
>    Once you have that, setting Inverse and Cascade All on the Person side
>    should do the trick.
>
>    Diego
>
>
>
>
>
>  --
> 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]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>

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