挽救一个孩子的家长存在家长、存在、孩子

2023-09-08 08:33:38 作者:谁家少年明媚如昨

我有一些继承了一个模型,它是使用NHibernate到persisti上的数据库。 NHibernate的映射与功能NHibernate工作正常,但我有一个场景,我要救一个孩子的存在父。我的模型是这样的:

I have a model with some inherits and it is using nhibernate to persisti on a Database. The nhibernate mapping with fluent nhibernate is working fine, but I have a scenario where I need to save a child for a existent parent. My model looks like this:

public class Item
{
   public long Id { get; set; }
   public string Name { get; set; }
   // other properties
}

public class ItemCommercial : Item
{
   public decimal Value { get; set; }
   // other properties
} 

在我的数据库,各表由相关ID和LT; - > ID为(每一个)。

In my Database, the respective tables are related by Id <-> Id (one per one).

我想知道,如何节约只是一个 ItemCommercial 实例上的数据库存在的项目。我有项目的ID,但我不知道howt说NHibernate的说刚子,而不是创建一个新的项目,样品:

I would like to know, how to Save just a ItemCommercial instance for a existent Item on database. I have the Id of the Item, but I do not know howt to say to nhibernate to say just the Child, instead creating a new Item, for sample:

session.Save(itemCommercialObj); // will create a Item and ItemCommercial with the same Id

感谢你。

推荐答案

由于我也answered这里

没有,这是不可能的升级已经坚持对象到它的子类。 NHibernate的根本不支持这一点。 如果安全具有相同ID作为基类的子类,NHibernate的只是创建创建引用成员与对象的新身份证复印件,而不是...

No, it is not possible to "upgrade" an already persisted object to its subclass. Nhibernate simply doesn't support this. If you safe the subclass with the same ID as the base class, Nhibernate simply creates a copy with a new ID of the object instead of creating the reference to Member...

因此​​,基本上,你可以做任何

So basically you could do either

复制客户的数据转换成会员,删除客户和保存会员 使用无子类不同的对象结构,其中一位是不同的表与它自己的ID和参考客户 使用本地SQL插入一行到会员...
 
精彩推荐
图片推荐