使用NHibernate在没有鉴别器的情况下为每个类层次结构保留一张表?
我有一个接口和一个实现该接口的类。
public interface IPhase {
string Description { get; set; }
int Id { get; }
string Phase { get; set; }
}
public class Phase : IPhase {
// Implementation here...
}
现在,使用 NHibernate 2.1.2.GA,我希望使用每个类层次结构表来映射接口及其实现器,实际上,我不需要鉴别器,因为此实现器将是唯一保留的类在此表中。我已经有大约一年半没有使用 NHibernate 了,我在这里遇到了一些记忆空白...
我已经阅读了这个相关的问题和答案,除了我没有使用 FNH。
NHibernate 映射:将层次结构保存到不带标识符的单个表
我想知道使用
子类
时是否必须使用discriminator
属性?在此特定上下文中我的 XML 映射应是什么样子?
衷心感谢您的帮助!
I have an interface and a class which implements this interface.
public interface IPhase {
string Description { get; set; }
int Id { get; }
string Phase { get; set; }
}
public class Phase : IPhase {
// Implementation here...
}
Now, using NHibernate 2.1.2.GA, I wish to use a table-per-class-hierarchy to map the interface and its implementor, and indeed, I don't need a discriminator, as this implementor will be the only class persisted in this table. I haven't used NHibernate for about a year and a half now, and I'm suffering some memory blanks here...
I have read this question and answers which is related, except I'm not using FNH.
NHibernate Mapping: Save hierarchy to single table without discriminator
I wonder whether the
discriminator
attribute is obligatory while usingsubclass
?What shall my XML mapping look like in this particular context?
Thanks kindly for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,NHibernate 需要一个用于表/类层次结构映射的鉴别器,否则当从数据库获取行时,它应该如何识别不同的子类呢?
如果接口只有一种实现,为什么要映射它呢?只需将类映射为普通实体(无需继承)。
编辑:忘记您可能在映射中引用了该接口。在这种情况下,您可以尝试使用
abstract="true"
标记基类(接口)的 table-per-concrete-class 映射,如下所述:http://nhibernate.info/doc/nh/en/index.html#inheritance-tableperconcreteOf course NHibernate needs a discriminator for a table-per-class-hierarchy mapping, how should it identify the different subclasses when getting a row from the database otherwise?
If there is only one implementation of the interface, why do you want to map it? Just map the class as a normal entity (without inheritance).
edit: Forgot that you might have references to the interface in your mapping. In that case you could try a table-per-concrete-class mapping marking the base class (interface) with
abstract="true"
like described here: http://nhibernate.info/doc/nh/en/index.html#inheritance-tableperconcrete