一对一和泛型:“关联引用未映射的类" [英] One-to-may and generics: "Association references unmapped class"

查看:104
本文介绍了一对一和泛型:“关联引用未映射的类"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR版本:下面的代码在构建SessionFactory时给了我一个"关联引用未映射的类"的异常.要对其进行修改,应在代码中进行哪些更改?

TL;DR version: The code below gives me a "Association references unmapped class" exception when building the SessionFactory. What should be changed in the code to fix it?

即使Ayende @ Rahien不建议在此中使用到通用类的映射2007年的文章我仍然可以尝试.我使用了他提到的方法.首先,起作用起作用:

Even though Ayende @ Rahien doesn't recommend using mappings to generic classes in this 2007 article I still had a go at it. I used the approach he mentioned. First the bit that is working:

<class name="Review`1[Person]" table="Review">
  <id name="Id" column="ReviewId"><generator class="native" /></id>
  <property name="Rating" />
  <many-to-one name="Subject" column="PersonId" class="Person" />
</class>

对应的通用类如下:

public class Review<T> : BaseEntity where T : IReviewable
{
    public virtual int Rating { get; set; }
    public virtual T Subject { get; set; }
}

这正在工作,我可以加载这些Review实体,并在我的MVC视图中显示它们,没有问题.令人高兴的是,当我在控制器和视图中使用Subject时,它的类型将正确.

This is working, and I can load these Review entities and show them in my MVC views no problem. The nice part is that my Subject will have the correct type when I use it in the controller and view.

但是,当尝试在我的Person类上映射一组Review项时,事情开始崩溃.这是我当前正在使用的映射:

However, when trying to map a collection of Review items on my Person class things start to break down. Here's the mapping I'm currently using:

<class name="Person">
  <!-- abbreviated -->
  <bag name="Reviews" table="Review">
    <key column="PersonId"/>
    <one-to-many class="Review`1[Person]" />
    <!-- Also tried these:
    <one-to-many class="table="Review"> 
    ... plus a few variations with fully qualified names ...
    -->
  </bag>
</class>

使用这个(缩写)类:

public class Person : BaseEntity, IReviewable
{
    /* Abbreviated */
    public virtual IList<Review<Person>> Reviews { get; set; }
}

这在构建SessionFactory时以异常结束:

This ends with an exception while building the SessionFactory:

协会引用了未映射的类:....在此处插入一对多的类....

Association references unmapped class: ....insert one-to-many.class here....

所以问题是:这里出了什么问题?这有可能吗?我应该放弃映射通用实体吗?

So the question: What's the problem here? Is this even possible? Should Ijust give up on mapping generic entities?

推荐答案

按代码映射为我生成了此代码:

Mapping-by-code generated this for me:

<class name="NHTest.Review`1[[NHTest.Person, NHTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" table="Review">
  ...
  <many-to-one name="Subject" column="..." />
</class>
<class name="Person">
  ...
  <bag name="Reviews">
    <key column="..." />
    <one-to-many class="NHTest.Review`1[[NHTest.Person, NHTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" />
  </bag>
</class>

因此,请尝试使用完全限定的名称.

So, try with fully-qualified names.

这篇关于一对一和泛型:“关联引用未映射的类"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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