2

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?

Joseph Nields
  • 5,527
  • 2
  • 32
  • 48

1 Answers1

1

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"...
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • this still isn't working. Edited the question. Any ideas? – Joseph Nields Oct 02 '15 at 20:16
  • Look it is in fact very easy.. Just provide FULL stack of the exception. NHibernate has one of the best exception logging ever. Please, provide full stack - that will give clear answer – Radim Köhler Oct 02 '15 at 20:17
  • It looks like it does work with the name property added. It says it's optional for an ID in the docs though! – Joseph Nields Oct 02 '15 at 20:25
  • Could be (I do not have experience with attribute mapping) But simply, the `` is too important to let it without proper information - what is the name of the ID property in C# – Radim Köhler Oct 02 '15 at 20:26
  • Well you can think of the attributes as decorators that generate XML. I.e. the xml that's generated from attributes is equiv to using an *.hbm.xml file. The NHibernate docs list an ID tag as not needing a name property – Joseph Nields Oct 02 '15 at 20:30
  • Yeah... But for me, that is exactly against the POCO principle, in my view (http://stackoverflow.com/a/13632872/1679310) - but if you like it... I really do not mind ;) Anyhow, do you have your mapping working? – Radim Köhler Oct 02 '15 at 20:31
  • 1
    Yeah! It's working now. Makes sense, but I usually just keep all the id's named "id" because it's easy to find them then. – Joseph Nields Oct 02 '15 at 20:36