简体   繁体   English

Hibernate / NHibernate:如何将子类持久化为超类的实例

[英]Hibernate/NHibernate : how to persist subclass as instance of superclass

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). 我想通过NHibernate保留两个类:-Cat,有一个名称和一个ID;-Kitten,它是Cat的子类(没有额外的公共属性)。

For stupid reasons, I would like to know if it is possible to map Cat only? 出于愚蠢的原因,我想知道是否可以仅映射Cat? So that when I persist Kitten, it is saved as a Cat and when I reload it, it is loaded as a Cat. 这样,当我坚持使用小猫时,它会被保存为Cat,而当我重新加载它时,它会被保存为Cat。

Additional constraints : - When trying, NHibernate crashed because it did not know Kitten. 其他限制:-尝试时,NHibernate崩溃了,因为它不了解小猫。 - However I do not want to add a discriminator value, as I do not want Hibernate to instantiate the Kitten subclass. -但是,我不想添加一个鉴别符值,因为我不想让Hibernate实例化Kitten子类。

Thank you very much! 非常感谢你!

EDIT : I forgot to write that using explicit ISession.Save("Cat", kitten) works. 编辑:我忘了写使用显式ISession.Save(“ Cat”,kitchen)的作品。 My problem occurs during a cascading save. 我的问题在级联保存期间发生。

老问题,我想我将不得不使用接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM