在 NHibernate 中映射嵌套类的 XML 语法是什么
假设您有以下类定义:
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 技术交流群。
发布评论
评论(2)
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>
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您必须对内部类使用 CLR 语法:
You have to use the CLR syntax for inner classes: