Search code examples
nhibernatemany-to-manynhibernate-mappingnhibernate-mapping-by-code

NHibernate Map By Code - Many To Many - Two way navigation


I'm pretty new to NHibernate.

I have working many to many mappings. they look like this:

public abstract class EntityMapping<T> : ClassMapping<T> where T : Entity
{
    public EntityMapping()
    {
        Id(x => x.Id, map => map.Generator(new IdentityGeneratorDef()));
    }
}

    public class PersonMapping : EntityMapping<Person>
    {
        public PersonMapping()
        {
            Property(x => x.Name);
        }
    }

     public class ParentMapping : JoinedSubclassMapping<Parent>
        {
            public ParentMapping()
            {
                Property(x => x.ParentOf);
                Key(x => x.Column("ParentId"));

                Bag(x => x.Children, map =>
                                         {
                                             map.Cascade(Cascade.All);
                                             map.Table("ParentChildren");
                                             map.Key(x => x.Column("ParentId"));
                                         },
                                         r => r.ManyToMany(rm => rm.Column("ChildId")));
            }
        }

        public class ChildMapping : JoinedSubclassMapping<Child>
        {
            public ChildMapping()
            {
                Property(x => x.Toys);
                Key(x => x.Column("ChildId"));

                Bag(x => x.Parents, map =>
                {
                    map.Cascade(Cascade.All);
                    map.Table("ParentChildren");
                    map.Inverse(true);
                    map.Key(x => x.Column("ChildId"));
                },
                r=>r.ManyToMany(rm => rm.Column("ParentId")));
            }
        }

When I do

var parent = session.Get<Parent>(1);
parent.Children.First(); // Works

I can access the children that have been added. However, when I try to navigate back the way ie:

var child = session.Get<Child>(2);
child.Parents.First(); // Doesn't work.

The parents collection is empty on the child object.

Is anyone able to shed some light on this issue?

Cheers,

James

EDIT

Sorry, I use the following code to save and get my entities:

using(var session = _sessionFactory.OpenSession())
{
   using(var transaction = session.BeginTransaction())
   {
      var person = new Parent(){Name = "James", ParentOf = 1};
      var child = new Child(){ Name="James Jr." Toys = 20 };

      person.Children.Add(child);

      session.Save(person);
      transaction.Commit();
   }
}

To retrieve data:

using(var session = _sessionFactory.OpenSession())
{
   using(var transaction = session.BeginTransaction())
   {
      var james = session.Get<Parent>(1);
      var child = session.Get<Child>(2);        

      transaction.Commit();
   }
}

Do I need to wrap the get's in a transaction?

Cheers,

James


Solution

  • Sorry I was being an idiot,

    I just wrote the same code again and noticed that I had the gets, in the same transaction as the commits (after the session.save and before the transaction.commit)

    Thanks for your help!