NHibernate.MappingException:No persister在程序集中找不到映射文档

时间:2010-08-02 19:56:06

标签: nhibernate nhibernate-mapping

我好久以来一直在做nhibernate。但今天我陷入了一个令人沮丧的问题,即映射异常。

    <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="IPC.Base.Domains" assembly="IPC">
  <class name="MenuItem" table="dbo.COR_MenuItem" default-access="property" default-cascade="save-update" default-lazy="true">
    <cache usage="read-only" region="completelyStatic"/>
    <id name="Id" type="System.Int32">
      <generator class="identity" />
    </id>
    <property name="Name" type="System.String" />
    <property name="Order" column="DisplayOrder" />
    <property name="Key" column="KeyChain" />
    <property name="Route" />
    <property name="ActionMethod" />
    <property name="IsHotlink" />
    <many-to-one name="ParentMenuItem" column="ParentMenuItemId" class="MenuItem" cascade="none"/>
    <bag name="MenuItems" table="dbo.COR_MenuItem" cascade="none">
      <cache usage="read-only" region="completelyStatic"/>
      <key column="ParentMenuItemId" />
      <one-to-many class="MenuItem" />
    </bag>
  </class>
</hibernate-mapping>

我确实有一个映射类如下:

using System;
using System.Collections.Generic;

namespace IPC.Base.Domain
{
    public partial class MenuItem : PersistentObject
    {
        public MenuItem()
        {
            MenuItems = new List<MenuItem>();
        }
        public virtual string Name { get; set; }
        public virtual int? Order { get; set; }
        public virtual string Key { get; set; }
        public virtual string Route { get; set; }
        public virtual string ActionMethod { get; set; }
        public virtual bool IsHotlink { get; set; }
        public virtual IList<MenuItem> MenuItems { get; set; }
        public virtual MenuItem ParentMenuItem { get; set; }

        /// <summary>
        /// only to be used from the menu builder app
        /// </summary>
        /// <param name="id"></param>
        public virtual void SetId(int id)
        {
            Id = id;
        }
    }
}

我在应用程序中运行了以下nHibernate Config。但根据我的经验,nHibernate配置绝对没有问题。

<?xml version="1.0"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
 <session-factory name="IPC">
  <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">**************SQL CONNECTION****************/property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    <property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
  <property name="cache.use_query_cache">true</property>
    <property name="show_sql">true</property>
    <mapping assembly="IPC"/>
 </session-factory>
</hibernate-configuration>

我使用以下行代码来调用nHibernate会话对象到系统。

static ISessionFactory CurrentFactory
        {
            get
            {
                if (factory == null)
                {
                    Configuration cfg = new Configuration();
                    if (cfgFile == null)
                        cfg = cfg.Configure();
                    else
                        cfg = cfg.Configure(cfgFile);

                    factory = cfg.BuildSessionFactory();
                }
                return factory;
            }
        }
        public static ISession Create()
        {
            var session = CurrentFactory.OpenSession();
            return session;
        }

现在!当我调用以下代码行时,我得到了:

public virtual T Get(int id)
        {
            return Session.Get<T>(id);
        }


No persister for: IPC.Base.Domain.MenuItem
Exception Details: NHibernate.MappingException: No persister for: IPC.Base.Domain.MenuItem

我已经通过nHibernate日志启用了。以下是日志

的基本细节
00:54:40.011 [4] INFO  NHibernate.Cfg.Environment - NHibernate 2.1.0.4000 (2.1.0.4000)
00:54:40.055 [4] INFO  NHibernate.Cfg.Environment - Bytecode provider name : lcg
00:54:40.057 [4] INFO  NHibernate.Cfg.Environment - Using reflection optimizer
00:54:40.682 [4] DEBUG NHibernate.Cfg.Configuration - dialect=NHibernate.Dialect.MsSql2005Dialect
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - connection.driver_class=NHibernate.Driver.SqlClientDriver
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - connection.connection_string=*********************SQL Connection*****************
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - proxyfactory.factory_class=NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - cache.provider_class=NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - cache.use_query_cache=true
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - show_sql=true
00:54:40.684 [4] DEBUG NHibernate.Cfg.Configuration - IPC<-IPC
00:54:40.685 [4] INFO  NHibernate.Cfg.Configuration - Searching for mapped documents in assembly: IPC
00:54:40.689 [4] WARN  NHibernate.Cfg.Configuration - No mapped documents found in assembly: IPC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
00:54:40.691 [4] INFO  NHibernate.Cfg.Configuration - Configured SessionFactory: IPC
00:54:40.691 [4] DEBUG NHibernate.Cfg.Configuration - properties: System.Collections.Generic.Dictionary`2[System.String,System.String]
00:54:45.716 [4] INFO  NHibernate.Cfg.Configuration - checking mappings queue
00:54:45.716 [4] INFO  NHibernate.Cfg.Configuration - processing one-to-many association mappings
00:54:45.717 [4] INFO  NHibernate.Cfg.Configuration - processing one-to-one association property references
00:54:45.717 [4] INFO  NHibernate.Cfg.Configuration - processing foreign key constraints
00:54:45.747 [4] INFO  NHibernate.Dialect.Dialect - Using dialect: NHibernate.Dialect.MsSql2005Dialect

我已经阅读了几乎所有的帖子! :)甚至谷歌也一样。我到达那里的最后一句话是我的程序集命名问题!我确定我遵循的步骤存在问题。但仍然在寻找那个触发器!

谢谢! 提前!!

2 个答案:

答案 0 :(得分:11)

可能您忘记将hbm映射设置为DLL的嵌入资源。

答案 1 :(得分:4)

最后我解决了这个问题!它基本上是一个愚蠢的东西,通常我倾向于做,然后忘记让它恢复运行阶段。

是的,我与装配问题有关。什么,但当我尝试构建数据层应用程序时,它没有在程序集内部嵌入xml文件。

  

解决问题的修复程序转到您的   * .hbm.xml文件并右键单击并更改该文件的构建选项   嵌入式资源。

Alrite!

非常感谢!