0
votes

I have three tables:

  1. Person (Id, FirstName)
  2. Organization (Id, Name)
  3. PersonOrganization (PersonId, OrganizationId, Details) many-to-many table

When I first mapped this using Fluent NHibernate I did not have a details column in the PersonOrganization table and mapped this using HasManyToMany in the PersonMap and OrganizationMap (no need to create a PersonOrganization domain object or map). I could writethe following code:

Organization org = new Organization { Name = "org" };
People people = new People { FirstName = "firstname", Organization = org };

peopleRepository.Add(people);          // ISession.Save(people)
unitOfWork.Commit();                   // ITransaction.Commit()

NHhibernate happily committed the data to all three tables.

The issue comes up when I added the details column in the PersonOrganization table. After some research it turns out that I now have to create a new PersonOrganization domain object and map and setup a HasMany relationship for both Person and Organization. My updated model and maps below:

public class People
{
    public People()
    {
        LinkToOrganization = new List<PeopleOrganization>();
    }

    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual IList<PeopleOrganization> LinkToOrganization { get; set; }
}

public class Organization
{
    public Organization()
    {
        LinkToPeople = new List<PeopleOrganization>();
    }

    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<PeopleOrganization> LinkToPeople { get; set; }
}

public class PeopleOrganization
{
    public virtual People People { get; set; }
    public virtual Organization Organization { get; set; }
    public virtual string Details { get; set; }
}

public class PeopleMap : ClassMap<People>
{
    public PeopleMap()
    {
        Id(p => p.Id);
        Map(p => p.FirstName).Length(100);

        HasMany(x => x.LinkToOrganization)
            .Table("PeopleOrganization")
            .KeyColumn("PeopleId")
            .AsBag()
            .Inverse();
    }
}

public class OrganizationMap : ClassMap<Organization>
{
    public OrganizationMap()
    {
        Id(p => p.Id);
        Map(p => p.Name).Length(50);

        HasMany(x => x.LinkToPeople)
            .Table("PeopleOrganization")
            .KeyColumn("OrganizationId")
            .AsBag()
            .Inverse();
    }
}

public class PeopleOrganizationMap : ClassMap<PeopleOrganization>
{
    public PeopleOrganizationMap()
    {
        CompositeId()
            .KeyReference(p => p.People, "PeopleId")
            .KeyReference(p => p.Organization, "OrganizationId");

        Map(p => p.Details).Length(100);
    }
}

I now have to write the following code:

People people = new People { FirstName = "firstname" };
Organization org = new Organization { Name = "org" };
PeopleOrganization po = new PeopleOrganization { People = people, Organization = org, Details = "details" };

peopleRepository.Add(people);          // ITransaction.Begin() ISession.Save(people)
organizationRepository.Add(org);       // ISession.Save(org)
peopleOrganizationRepository.Add(po);  // ISession.Save(po)

unitOfWork.Commit();                   // ITransaction.Commit()

My questions are:

  1. Are my mappings correctly setup to support this kind of many-to-many scenario?
  2. Is there are way for me to just be able to do the following (which would write to all three tables):

-

People people = new People { FirstName = "firstname" };
Organization org = new Organization { Name = "org" };
PeopleOrganization po = new PeopleOrganization { People = people, Organization = org, Details = "details" };

peopleRepository.Add(people);          // Just ONE ISession.Save(people)

unitOfWork.Commit();                   // ITransaction.Commit()

Any input is highly appreciated.

Thanks

1

1 Answers

1
votes

Firstly, you'll need to add your newly created PeopleOrganization to the collection on both entities (Organization and People). Then if you add a Cascade.All() to your HasMany chain, the saves should propagate (cascade) down to the PeopleOrganization and subsequently to Organization.

As a purely semantic suggestion, I'd recommend encapsulating the PeopleOrganization creation into a method on Person. Something like this:

class Person
{
  public void AddOrganization(Organization org, string details)
  {
    var link = new PeopleOrganization { ... };

    LinkToOrganization.Add(link)
    org.LinkToPeople.Add(link);
  }
}

That way you never have to deal with creating the intrim entity yourself.