在 NHibernate 中映射嵌套类的 XML 语法是什么

发布于 10-06 14:10 字数 1303 浏览 6 评论 0原文

假设您有以下类定义:

public class SomeEntity
{
    public class Key
    {
        public virtual OtherEntity Other { get; set; }
        public virtual int Index { get; set; }

        public override bool Equals(object other)
        {
            // something here...
        }

        public override int GetHashCode()
        {
            // something here...
        }
    }

    public virtual Key Id { get; set; }
}

public class OtherEntity
{
    public virtual int Id { get; set; }
}

然后您希望有一个类似于以下内容的映射文件:

<class name="SomeEntity" table="SOME">

  <composite-id name="Id" class="SomeEntity.Key">
    <key-many-to-one name="Other" column="OTHER_ID" class="OtherEntity" />
    <key-property name="Index" column="INDEX" type="int" />
  </composite-id>

</class>

<class name="OtherEntity" table="OTHER">

  <id name="Id" column="ID" type="int">
    <generator class="identity" />
  </id>

</class>

尝试初始化 NHibernate 会导致抛出 NHibernate.MappingException ,并显示消息 “找不到类:SomeEntity.Key”。问题很可能出在 class="SomeEntity.Key" 属性上。我无法找到引用嵌套类的正确语法。

另外,我想得到一个提示,我应该在哪里寻找答案(例如,我在“NHibernate in Action”书中找不到答案)。对于此类有关 NHibernate 的问题,您首选的资源是什么?

Suppose you have the following class definitions:

public class SomeEntity
{
    public class Key
    {
        public virtual OtherEntity Other { get; set; }
        public virtual int Index { get; set; }

        public override bool Equals(object other)
        {
            // something here...
        }

        public override int GetHashCode()
        {
            // something here...
        }
    }

    public virtual Key Id { get; set; }
}

public class OtherEntity
{
    public virtual int Id { get; set; }
}

And then you would like to have a mapping file similar to the one below:

<class name="SomeEntity" table="SOME">

  <composite-id name="Id" class="SomeEntity.Key">
    <key-many-to-one name="Other" column="OTHER_ID" class="OtherEntity" />
    <key-property name="Index" column="INDEX" type="int" />
  </composite-id>

</class>

<class name="OtherEntity" table="OTHER">

  <id name="Id" column="ID" type="int">
    <generator class="identity" />
  </id>

</class>

Trying to initialize NHibernate results in an NHibernate.MappingException being thrown, whith the message of "could not find class: SomeEntity.Key". Most probably the problem is with the class="SomeEntity.Key" attribute. I was unable to find the proper syntax for referencing a nested class.

Also I would like to get a hint where else should I have been looking for the answer (I couldn't find the answer in the "NHibernate in Action" book, for example). What is your preferred resource for such questions about to NHibernate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒与心事2024-10-13 14:10:17

您必须对内部类使用 CLR 语法:

<composite-id name="Id" class="SomeEntity+Key">
...

You have to use the CLR syntax for inner classes:

<composite-id name="Id" class="SomeEntity+Key">
...
久隐师2024-10-13 14:10:17
public class MainClass
{
  public virtual long MainKey {get; set;}
  public virtual SubClass SubInstance {get; set;}

  public class SubClass
  {
    public virtual long SubKey {get;set;}
  }
}

可以映射为:

<class name="MainClass" table="Main">
  <id name="MainKey" column="MainId" type="Int64">
    <generator class="identity" />
  </id>
  <many-to-one name="SubInstance" class="MainClass+SubClass" Column="SubId"/> 
</class>

<class name="MainClass+SubClass" table="Sub">
  <id name="SubKey" column="SubId" type="Int64">
    <generator class="identity" />
  </id>
</class>
public class MainClass
{
  public virtual long MainKey {get; set;}
  public virtual SubClass SubInstance {get; set;}

  public class SubClass
  {
    public virtual long SubKey {get;set;}
  }
}

can be mapped as:

<class name="MainClass" table="Main">
  <id name="MainKey" column="MainId" type="Int64">
    <generator class="identity" />
  </id>
  <many-to-one name="SubInstance" class="MainClass+SubClass" Column="SubId"/> 
</class>

<class name="MainClass+SubClass" table="Sub">
  <id name="SubKey" column="SubId" type="Int64">
    <generator class="identity" />
  </id>
</class>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文