I am just starting to learn NHibernate now, I tried setting up a very
simple example to play around and for some reason I cannot get
cascading to work.  I followed the example of the FirstExample project
and tried to apply it to my domain.  I have two classes with a one-to-
many relationship between them.  They are Variable and Relation
(related to metadata we store in our environment).

The problem is that when I run the unit test which should save the
relation and the related variable only the relation ever gets saved,
if at all.

I have tried what seems to me to be every variation of the mappings to
see if they would work but nothing has succeeded.  The weird thing is,
the FirstExample project (for both SQLite and when I point it to SQL
Server Express) seem to work fine.  There is something I am
misunderstanding in my mappings.  I even tried switching between using
Save and SaveOrUpdate on the session variable.  I'm using the trunk of
the fluent-nhibernate project, including the nhibernate library in its
tools directory (2.0.1)

Any help is greatly appreciated and sorry if its a bit of a newb
question but its really bugging me!

Here are the classes for reference:

    public class Variable : IEntity
    {
        public virtual long Id
        {
            get;
            set;
        }

        public virtual Relation ParentRelation
        {
            get;
            set;
        }
    }

    public class Relation : IEntity
    {
        public virtual long Id
        {
            get;
            set;
        }

        public virtual IList<Variable> Variables
        {
            get;
            set;
        }

        public Relation()
        {
            Variables = new List<Variable>();
        }

        public virtual void AddQuestion(Variable item)
        {
            item.ParentRelation = this;
            Variables.Add(item);
        }
    }

and here are the mappings:

    public class VariableMap : ClassMap<Variable>
    {
        public VariableMap()
        {
            Id(x => x.Id);
            References(x => x.ParentRelation);
        }
    }

    public class RelationMap : ClassMap<Relation>
    {
        public RelationMap()
        {
            Id(x => x.Id);
            HasMany(x => x.Variables)
                .Inverse()
                .Cascade.All();
        }
    }

I have a little session provider to configure everything here:

    public class MySessionProvider
    {
        private NHibernate.ISessionFactory _sessionFactory;
        private NHibernate.Cfg.Configuration _configuration;

        public MySessionProvider()
        {
            _sessionFactory = Fluently.Configure()
                                    .Database(
                                        MsSqlConfiguration.MsSql2005
                                            .ConnectionString(x => x.Is
(@"Server=BUMBLEBEE
\SQLEXPRESS;Database=EditImputation;Trusted_Connection=True;"))
                                            .ShowSql()
                                    )
                                    .Mappings(x =>
x.FluentMappings.AddFromAssemblyOf<Relation>())
                                    .ExposeConfiguration
(StoreConfiguration)
                                    .BuildSessionFactory();
        }

        private void StoreConfiguration(NHibernate.Cfg.Configuration
configuration)
        {
            _configuration = configuration;
        }

        public NHibernate.ISession CreateSession()
        {
            return _sessionFactory.OpenSession();
        }

        public void BuildSchema()
        {
            new NHibernate.Tool.hbm2ddl.SchemaExport
(_configuration).Create(false, true);
        }
    }

And I run everything in a unit test like this:

        [TestMethod]
        public void Can_Add_Variable()
        {
            var relation = new Relation();
            relation.Id = 9;

            var variable = new Variable();
            variable.Id = 99;

            relation.AddQuestion(variable);

            using (var session = sessionProvider.CreateSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(relation);
                }
            }
       }


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" 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/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to