Search code examples
fluent-nhibernatefluent-nhibernate-mapping

Parent-Child mapping with Fluent NHibernate does not insert children


I'm trying to map a very basic parent-child relation with Fluent NHibernate. However, when analyzing the SQL, only the parent-INSERT statement is created.

The situation is a simple class with a list of other classes. No relation back to the parent is needed. The children needs to be inserted/updated when the parent is inserted/updated.

        var room = new Room();
        room.Name = "Room1";
        room.Courses.Add(new Course(){ Name = "Course1"});
        room.Courses.Add(new Course(){ Name = "Course2"});

        using (var session = sessionFactory.OpenStatelessSession())
        {
            using (var transaction = session.BeginTransaction())
            {
                session.Insert(room);
                transaction.Commit();
            }
        }

The mapping looks like this.

public class RoomMapping : ClassMap<Room>
{
    public RoomMapping()
    {
        Table("Rooms");

        Id(x => x.Id)
            .GeneratedBy.SeqHiLo("seq_rooms", "1000");

        Map(x => x.Name);

        HasMany(x => x.Courses)
            .Cascade.All();
    }
}

public class CourseMap : ClassMap<Course>
{
    public CourseMap()
    {
        Table("Courses");

        Id(x => x.Id)
            .GeneratedBy.SeqHiLo("seq_courses", "1000");

        Map(x => x.Name);
    }
}

I already played with multiple options of the HasMany, however non with any success.


Solution

  • Sorry people. I just found it out. I'm working in a Stateless session. So no relationships are managed ;)