且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-30 16:54:34

老问题,我想我必须改用接口。


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.