Hibernate / NHibernate:如何将子类作为超类的实例持久化 [英] Hibernate/NHibernate : how to persist subclass as instance of superclass
问题描述
我有两个类,我想通过NHibernate持久化:
- Cat,它有一个名称和一个Id,
- Kitten,它是Cat的子类(没有额外的公共属性)。
出于愚蠢的原因,我想知道是否可以仅映射Cat?所以当我坚持Kitten的时候,它被保存为一个Cat,当我重新加载它时,它被加载为一个Cat。
附加约束:
- 当尝试,NHibernate崩溃,因为它不知道小猫。
- 但是我不想添加歧义值,因为我不想让Hibernate实例化Kitten子类。
非常感谢! p>
编辑:
我忘了写明确使用ISession.Save(Cat,kitten)。我的问题发生在级联保存期间。
老问题,我想我必须改用接口。
I have two classes I would like to persist via NHibernate: - Cat, which has a name and an Id, - Kitten, which is a subclass of Cat (no extra public properties).
For stupid reasons, I would like to know if it is possible to map Cat only? So that when I persist Kitten, it is saved as a Cat and when I reload it, it is loaded as a Cat.
Additional constraints : - When trying, NHibernate crashed because it did not know Kitten. - However I do not want to add a discriminator value, as I do not want Hibernate to instantiate the Kitten subclass.
Thank you very much!
EDIT : I forgot to write that using explicit ISession.Save("Cat", kitten) works. My problem occurs during a cascading save.
Old question, I guess I would have to use interfaces instead.
这篇关于Hibernate / NHibernate:如何将子类作为超类的实例持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!