NHibernate could not compile the mapping

2019-08-08 11:28发布

I don't know where is error.

I use NHibernate C# and append Assembly to Configuration. Problem a NHibernateException: Could not compile the mapping Note.hbm.xml

public static class NHibernateHelper
{
    private static Configuration _cfg;
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory {
        get {
            if (_sessionFactory == null) {
                _cfg = new Configuration();
                _cfg.Configure();

                _cfg.AddAssembly(typeof(User).Assembly);
                _cfg.AddAssembly(typeof(Changelog).Assembly);
                _cfg.AddAssembly(typeof(Note).Assembly);


                _sessionFactory = _cfg.BuildSessionFactory();
            }

            return _sessionFactory;
        }
    }


    public static ISession OpenSession() {
        return SessionFactory.OpenSession();
    }

    public static void DatabaseSchemaLoader() {
        ISession session = OpenSession();
        new SchemaExport(_cfg).Create(true, true);
        session.Close();
    }
}

My Note class is shared.data.content

namespace CRMSystem.shared.data.content
{
    public class Note
    {    
        public virtual int id { get; set; }
        public virtual string title{ get; set;}
        public virtual string text { get; set; }
        public virtual int priority { get; set; }
        public virtual long createdTime { get; set; }
        public virtual long lastEditTime { get; set; }    
    }
}

Note.hbm.xml (set build action: embedded resource, copy to output: copy always)

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="CRMSystem"
                   namespace="CRMSystem.shared.data.content">

  <class name="Note" table="`Note`" lazy="false">


    <id name="id" column="`id`" type="int">
      <generator class="identity"></generator>
    </id>

    <property name="title" column="`title`" type="String"/>
    <property name="text" column="`text`" type="String" />
    <property name="priority" column="`priority`" type="int" />
    <property name="createdTime" column="`createdTime`" type="long" />
    <property name="lastEditTime" column="`lastEditTime`" type="long" />
  </class>
</hibernate-mapping>

Thanks for your answer.

I have tried today in many ways and found to that it works:

_cfg.Configure();
_cfg.AddAssembly(typeof(User).Assembly);
//_cfg.AddAssembly(typeof(Changelog).Assembly);
//_cfg.AddAssembly(typeof(Note).Assembly);

When I adds: (any class)

_cfg.AddAssembly(typeof(Changelog).Assembly);

Every I get an error: Could not compile Note.hbm.xml I can save to database every class (User, Changelog, Note). I can't explain.

Has anyone ever have such situations ?

Full exception:

    NHibernate.MappingException: Could not compile the mapping document: CRMSystem.shared.data.mapping.Note.hbm.xml ---> NHibernate.DuplicateMappingException: Duplicate class/entity mapping CRMSystem.shared.data.content.Note
   w NHibernate.Cfg.Mappings.AddClass(PersistentClass persistentClass)
   w NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(HbmClass classSchema, IDictionary`2 inheritedMetas)
   w NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(HbmClass rootClass, IDictionary`2 inheritedMetas)
   w NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddEntitiesMappings(HbmMapping mappingSchema, IDictionary`2 inheritedMetas)
   w NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(HbmMapping mappingSchema)
   w NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
   --- Koniec śladu stosu wyjątków wewnętrznych ---
   w NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
   w NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
   w NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
   w NHibernate.Cfg.Configuration.ProcessMappingsQueue()
   w NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document)
   w NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name)
   w NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
   w NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
   w NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)

When I nothing add , i get exception : no persister

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-08 12:17

EXTEND

Based on the latest update, we can see, that NHiberante is blaming at:

... Duplicate class/entity mapping CRMSystem.shared.data.content.Note ...

And that means, that in our case, some other file could contain the NOTE mapping, e.g. in user.hbm.xml there could be some forgotten mapping

<class name="Note" ...

ORIGINAL part

The code is ok as is. I tried locally with your Note class and Note.hbm.xml. Working as expected.

The issue seems to be some misspelling which I can hardly reproduce (e.g. different assembly name). Or, despite of the blame on the Note.hbm.xml ... it also could be some other mapping

// there could be also some issue
_cfg.AddAssembly(typeof(User).Assembly);
_cfg.AddAssembly(typeof(Changelog).Assembly);

Anyhow, try to observe the INNER exception. There will be clear issue description. E.g. for this mapping

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               // instead of this
               // assembly="CRMSystem"
               // we have this
               assembly="WrongAssembly"
               namespace="CRMSystem.shared.data.content">

We would receive full exception:

NHibernate.MappingException: Could not compile the mapping document: ...Note.hbm.xml ---> NHibernate.MappingException: persistent class
CRMSystem.shared.data.content.Note, WrongAssembly not found ---> System.IO.FileNotFoundException: Could not load file or assembly 'WrongAssembly' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF.

Note: embedded resource property is must, do not copy is suggested. This file will be part of dll, it does not have to be copied as a file.

查看更多
登录 后发表回答