Search code examples
c#hibernatenhibernatenhibernate-mapping

Many-to-One Mapping in NHibernate Not Working?


I have the following example persistent classes:

using NHibernate.Mapping.Attributes;

namespace GumiDAL.Domain
{
    [Class]
    public class Foo
    {
        [Id(0)]
        [Generator(1, Class="identity")]
        public virtual int Id { get; set; }

        [Property]
        public virtual string Name { get; set; }

    }

    [Class]
    public class Bar
    {
        [Id(0)]
        [Generator(1, Class = "identity")]
        public virtual int Id { get; set; }


        [ManyToOne(Name="foo")]
        public virtual Foo foo { get; set; }
    }

}

Serializing the assembly creates the following xml:

<!--
Generated from NHibernate.Mapping.Attributes on 2015-10-02 13:08:49Z.
-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="Example.Domain.Foo, ExampleAssembly">
        <id>
            <generator class="identity"/>
        </id>
        <property name="Name"/>
    </class>
    <class name="Example.Domain.Bar, ExampleAssembly">
        <id>
            <generator class="identity"/>
        </id>
        <many-to-one name="foo"/>
    </class>
</hibernate-mapping>

But when I try to set up a configuration, I get an exception with the following message and stack trace:

Could not compile deserialized mapping document.

A first chance exception of type 'NHibernate.MappingException' occurred in NHibernate.dll
   at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 344
   at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 532
   at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 501
   at NHibernate.Cfg.Configuration.ProcessMappingsQueue() in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 1867
   at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 1858
   at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 1851
   at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 640
   at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 614
   at Example.Domain.Tests.SetUp()

Everything looks correct in the xml to me... any ideas what I'm missing?


Solution

  • The name="" attribute represents C# property. So, we should use

    // instead of this
    <many-to-one name="foo_id"/>
    // we need this
    <many-to-one name="foo" column="foo_id"/>
    

    So, name of the property is foo not foo_id. I guess that we would need mapping like this:

    //[ManyToOne(Name="foo_id")]
    [ManyToOne(Column="foo_id")]
    public virtual Foo foo { get; set; }
    

    With a full stack exception it would be easier, but also, the <id> element should have the name:

    <id name="Id" column="foo_id"...