C# NHibernate.MappingException:没有持久性:XYZ

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/57804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:14:18  来源:igfitidea点击:

NHibernate.MappingException: No persister for: XYZ

提问by Sara Chipps

Now, before you say it: I didGoogle and my hbm.xmlfile isan Embedded Resource.

现在,在你说之前:我做了谷歌,我的hbm.xml文件一个嵌入式资源。

Here is the code I am calling:

这是我正在调用的代码:

ISession session = GetCurrentSession();
var returnObject =  session.Get<T>(Id);

Here is my mapping file for the class:

这是我的类映射文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="HQData.Objects.SubCategory, HQData" table="SubCategory" lazy="true">
    <id name="ID" column="ID" unsaved-value="0">
      <generator class="identity" />
    </id>

    <property name="Name" column="Name" />
    <property name="NumberOfBuckets" column="NumberOfBuckets"  />
    <property name="SearchCriteriaOne" column="SearchCriteriaOne" />

    <bag name="_Businesses" cascade="all">
      <key column="SubCategoryId"/>
      <one-to-many 
         class="HQData.Objects.Business, HQData"/>
    </bag>

    <bag name="_Buckets" cascade="all">
      <key column="SubCategoryId"/>
      <one-to-many
         class="HQData.Objects.Bucket, HQData"/>
    </bag>

  </class>
</hibernate-mapping>

Has anyone run to this issue before?

有没有人遇到过这个问题?

Here is the full error message:

这是完整的错误消息:

MappingException: No persister for:  HQData.Objects.SubCategory]NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName, Boolean throwIfNotFound)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionFactoryImpl.cs:766 NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionFactoryImpl.cs:752 NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Event\Default\DefaultLoadEventListener.cs:37 NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionImpl.cs:2054 NHibernate.Impl.SessionImpl.Get(String entityName, Object id)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionImpl.cs:1029 NHibernate.Impl.SessionImpl.Get(Type entityClass, Object id)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionImpl.cs:1020 NHibernate.Impl.SessionImpl.Get(Object id)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionImpl.cs:985 HQData.DataAccessUtils.NHibernateObjectHelper.LoadDataObject(Int32 Id)
 in C:\Development\HQChannelRepo\HQ Channel Application\HQChannel\HQData\DataAccessUtils\NHibernateObjectHelper.cs:42 HQWebsite.LocalSearch.get_subCategory()
 in C:\Development\HQChannelRepo\HQ Channel Application\HQChannel\HQWebsite\LocalSearch.aspx.cs:17 HQWebsite.LocalSearch.Page_Load(Object sender, EventArgs e)
 in C:\Development\HQChannelRepo\HQ Channel Application\HQChannel\HQWebsite\LocalSearch.aspx.cs:27 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +47 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436
MappingException: No persister for:  HQData.Objects.SubCategory]NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName, Boolean throwIfNotFound)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionFactoryImpl.cs:766 NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionFactoryImpl.cs:752 NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Event\Default\DefaultLoadEventListener.cs:37 NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionImpl.cs:2054 NHibernate.Impl.SessionImpl.Get(String entityName, Object id)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionImpl.cs:1029 NHibernate.Impl.SessionImpl.Get(Type entityClass, Object id)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionImpl.cs:1020 NHibernate.Impl.SessionImpl.Get(Object id)
 in c:\CSharp\NH2.0.0\nhibernate\src\NHibernate\Impl\SessionImpl.cs:985 HQData.DataAccessUtils.NHibernateObjectHelper.LoadDataObject(Int32 Id)
 in C:\Development\HQChannelRepo\HQ Channel Application\HQChannel\HQData\DataAccessUtils\NHibernateObjectHelper.cs:42 HQWebsite.LocalSearch.get_subCategory()
 in C:\Development\HQChannelRepo\HQ Channel Application\HQChannel\HQWebsite\LocalSearch.aspx.cs:17 HQWebsite.LocalSearch.Page_Load(Object sender, EventArgs e)
 in C:\Development\HQChannelRepo\HQ Channel Application\HQChannel\HQWebsite\LocalSearch.aspx.cs:27 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +47 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

Update, here's what the solution for myscenario was: I had changed some code and I wasn't adding the Assembly to the config file during runtime.

更新,这里就是为解决我的情况是:我已经改变了一些代码,我在运行时没有加入大会的配置文件。

采纳答案by Andy S

Sounds like you forgot to add a mapping assembly to the session factory configuration..

听起来您忘记将映射程序集添加到会话工厂配置中。

If you're using app.config...

如果你使用 app.config ...

.
.
    <property name="show_sql">true</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <mapping assembly="Project.DomainModel"/>  <!-- Here -->
</session-factory>
.
.

回答by Matt Hinze

Should it be name="Id"? Typos are a likely cause.

应该是name="Id"吗?错别字是一个可能的原因。

Next would be to try it out with a non-generic test to make sure you're passing in the proper type parameter.

接下来是使用非通用测试进行尝试,以确保您传入了正确的类型参数。

Can you post the entire error message?

你能发布整个错误信息吗?

回答by Andy S

If running tests on the repository from a seperate assembly, then make sure your Hibernate.cfg.xml is set to output always in the bin directory of said assembly. This wasn't happening for us and we got the above error in certain circumstances.

如果从单独的程序集对存储库运行测试,请确保您的 Hibernate.cfg.xml 设置为始终在所述程序集的 bin 目录中输出。这对我们来说没有发生,我们在某些情况下会出现上述错误。

Disclaimer: This might be a slightly esoteric bit of advice, given that it's a direct result of how we structure our repository integration test assemblies (i.e. we have a symbolic link from each test assembly to a single Hibernate.xfg.xml)

免责声明:这可能是一个有点深奥的建议,因为它是我们如何构建存储库集成测试程序集的直接结果(即我们有从每个测试程序集到单个 Hibernate.xfg.xml 的符号链接)

回答by Andy S

Had a similar problem when find an object by id... All i did was to use the fully qualified name in the class name. That is Before it was :

通过 id 查找对象时遇到了类似的问题......我所做的只是在类名中使用完全限定名称。那是之前:

find("Class",id)

Object so it became like this :

对象所以它变成了这样:

find("assemblyName.Class",id)

回答by Chris Vosnidis

Something obvious, yet quite useful for someone new to NHibernate.

一些显而易见的东西,但对 NHibernate 的新手来说非常有用。

All XML Mapping files should be treated as Embedded Resourcesrather than the default Content. This option is set by editing the Build Action attribute in the file's properties.

所有 XML 映射文件都应该被视为嵌入式资源而不是默认的Content。此选项是通过编辑文件属性中的构建操作属性来设置的。

XML files are then embedded into the assembly, and parsed at project startup during NHibernate's configuration phase.

然后将 XML 文件嵌入到程序集中,并在 NHibernate 的配置阶段在项目启动时进行解析。

回答by Chris Vosnidis

Don't forget to specify mapping information in .config file

不要忘记在 .config 文件中指定映射信息

e.g.

例如

where MyApp.Data is assembly that contains your mappings

其中 MyApp.Data 是包含您的映射的程序集

回答by David

I had similar problem, and I solved it as folows:

我有类似的问题,我解决它如下:

I working on MS SQL 2008, but in the NH configuration I had bad dialect: NHibernate.Dialect.MsSql2005Dialectif I correct it to: NHibernate.Dialect.MsSql2008Dialectthen everything's working fine without a exception "No persister for: ..." David.

我在 MS SQL 2008 上工作,但在 NH 配置中,我的方言很糟糕:NHibernate.Dialect。MsSql2005Dialect如果我将其更正为:NHibernate.Dialect。MsSql2008Dialect然后一切正常,无一例外“没有持久性:......”大卫。

回答by nHibernate User

My issue was that I forgot to put the .hbm in the name of the mapping xml. Also make sure you make it an embedded resource!

我的问题是我忘记将 .hbm 放在映射 xml 的名称中。还要确保将其设为嵌入式资源!

回答by basarat

I got this off of here:

我从这里得到了这个:

In my case the mapping class was not public. In other words, instead of:

在我的情况下,映射类不是公开的。换句话说,而不是:

public class UserMap : ClassMap<user>  // note the public!

I just had:

我刚有:

class UserMap : ClassMap<user>

回答by IdontCareAboutReputationPoints

I had the same problem because I was adding the wrong assembly in Configuration.AddAssembly() method.

我遇到了同样的问题,因为我在 Configuration.AddAssembly() 方法中添加了错误的程序集。