NHibernate无法编译映射 [英] NHibernate could not compile the mapping

查看:92
本文介绍了NHibernate无法编译映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道哪里出错了.

我使用NHibernate C#并将程序集附加到Configuration. NHibernateException问题:无法编译映射Note.hbm.xml

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();
    }
}

我的Note类是shared.data.content

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(设置构建操作:嵌入式资源,复制到输出:始终复制)

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);

每个我都会收到一个错误:无法编译Note.hbm.xml 我可以将每个类(User,Changelog,Note)保存到数据库. 我无法解释.

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 ?

完全例外:

    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

推荐答案

EXTEND

根据最新更新,我们可以看到NHiberante的原因是:

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

...重复的类/实体映射CRMSystem.shared.data.content.Note ...

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

这意味着,在我们的例子中,其他文件可能包含NOTE映射,例如在user.hbm.xml中可能有一些被遗忘的映射

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" ...

原始部分

原样是可以的.我在您的 Note 类和 Note.hbm.xml 上进行了本地尝试.正常工作.

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

问题似乎是拼写错误,我几乎无法复制(例如,不同的程序集名称).或者,尽管对Note.hbm.xml负责,也可能是其他一些映射

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);

无论如何,请尝试观察INNER异常.将会有明确的问题描述.例如.对于此映射

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">

我们将收到全部例外:

NHibernate.MappingException:无法编译映射文档:... Note.hbm.xml ---> NHibernate.MappingException:持久类
CRMSystem.shared.data.content.注意,找不到WrongAssembly ---> System.IO.FileNotFoundException:无法加载文件或程序集"WrongAssembly"或其依赖项之一.系统找不到指定的文件.WRN:程序集绑定日志记录已关闭.

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.

注意:必须具有嵌入资源属性,建议请勿复制.该文件将成为dll的一部分,不必将其复制为文件.

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.

这篇关于NHibernate无法编译映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆