MappingException未处理:NHibernate

时间:2011-03-23 17:06:08

标签: .net visual-studio-2010 nhibernate c#-4.0

我试图从书NHibernate in Action中学习NHibernate并试图编译并运行第一个例子给我以下异常:

Could not compile the mapping document:
     

Namespace.FolderName.ClassName.hbm.xml

内部异常是

{"<hibernate-mapping xmlns='urn:hibernate-mapping-2.2'> was not expected."}

这是我的映射文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:hibernate-mapping-2.2"
                   auto-import="true">
  <class name="Namespace.Folder.ClassName,Namespace.Folder" lazy="false">
    <id name="id" access="field">
      <generator class="native" />
    </id>
    <property name="name" access="field" column="name"/>
    <many-to-one access="field" name="manager" column="manager"
                 cascade="all"/>
    </class>
</hibernate-mapping>

当我尝试按如下方式创建会话时会发生这种情况:

static ISession OpenSession()
        {
            if (factory == null)
            {
                Configuration c = new Configuration();
                c.AddAssembly(Assembly.GetCallingAssembly());
                factory = c.BuildSessionFactory();
            }
            return factory.OpenSession();
        }

以下行引发了异常:

c.AddAssembly(Assembly.GetCallingAssembly());

这是整个异常堆栈跟踪:

**

  

NHibernate.MappingException是   未处理的消息=无法编译   映射文件:   DataGenerator.HibernateMapper.Individual.hbm.xml   Source = NHibernate StackTrace:          在NHibernate.Cfg.Configuration.LogAndThrow(例外   例外)   d:\ CSHARP \ NH \的nhibernate \ SRC \ NHibernate的\ CFG \ Configuration.cs:线   340          在NHibernate.Cfg.Configuration.LoadMappingDocument(XmlReader   hbmReader,String name)in   d:\ CSHARP \ NH \的nhibernate \ SRC \ NHibernate的\ CFG \ Configuration.cs:线   1783          在NHibernate.Cfg.Configuration.AddXmlReader(XmlReader   hbmReader,String name)in   d:\ CSHARP \ NH \的nhibernate \ SRC \ NHibernate的\ CFG \ Configuration.cs:线   1813          在NHibernate.Cfg.Configuration.AddInputStream(Stream   xmlInputStream,String name)in   d:\ CSHARP \ NH \的nhibernate \ SRC \ NHibernate的\ CFG \ Configuration.cs:线   630          在NHibernate.Cfg.Configuration.AddResource(String   路径,装配组件)   d:\ CSHARP \ NH \的nhibernate \ SRC \ NHibernate的\ CFG \ Configuration.cs:线   668          在NHibernate.Cfg.Configuration.AddAssembly(汇编   装配)in   d:\ CSHARP \ NH \的nhibernate \ SRC \ NHibernate的\ CFG \ Configuration.cs:线   761          在DataGenerator.Program.OpenSession()中   C:\ Documents and   设置\ USER1 \桌面\ DataGeneration \ DataGenerator \ DataGenerator \的Program.cs:行   46          在DataGenerator.Program.CreateIndividualAndSaveToDatabase()   在C:\ Documents和   设置\ USER1 \桌面\ DataGeneration \ DataGenerator \ DataGenerator \的Program.cs:行   29          在C:\ Documents和的DataGenerator.Program.Main()中   设置\ USER1 \桌面\ DataGeneration \ DataGenerator \ DataGenerator \的Program.cs:行   16          在System.AppDomain._nExecuteAssembly(RuntimeAssembly   assembly,String [] args)          在System.AppDomain.ExecuteAssembly(String   assemblyFile,Evidence   assemblySecurity,String [] args)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          在System.Threading.ThreadHelper.ThreadStart_Context(Object   州)          在System.Threading.ExecutionContext.Run(ExecutionContext   executionContext,ContextCallback   回调,对象状态,布尔值   ignoreSyncCtx)          在System.Threading.ExecutionContext.Run(ExecutionContext   executionContext,ContextCallback   回调,对象状态)          在System.Threading.ThreadHelper.ThreadStart()   的InnerException:   System.InvalidOperationException          消息= XML文档中存在错误(1,2)。          来源=的System.Xml          堆栈跟踪:               在System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader   xmlReader,String encodingStyle,   XmlDeserializationEvents事件)               在System.Xml.Serialization.XmlSerializer.Deserialize(TextReader   的TextReader)               在NHibernate.Cfg.NamedXmlDocument..ctor(String   名称,XmlDocument文档)   d:\ CSHARP \ NH \的nhibernate \ SRC \ NHibernate的\ CFG \ NamedXmlDocument.cs:线   27               在NHibernate.Cfg.Configuration.LoadMappingDocument(XmlReader   hbmReader,String name)in   d:\ CSHARP \ NH \的nhibernate \ SRC \ NHibernate的\ CFG \ Configuration.cs:线   1774          InnerException:System.InvalidOperationException               消息=是   没想到。               来源= 2p514b3b               堆栈跟踪:                    在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderHbmMapping.Read109_hibernatemapping()               的InnerException:

**

有人可以就这里发生什么事给我一些指示吗?

由于

1 个答案:

答案 0 :(得分:4)

确保映射文件构建操作设置为“嵌入式资源”。

编辑:

尝试

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">

请注意nhibernate中的