简体   繁体   English

NHibernate 在合并之前保存嵌套的瞬态对象

[英]NHibernate saving nested transient objects, before merge

Is there a way to save transient objects within a nested object.有没有办法在嵌套对象中保存瞬态对象。

The problem is that the object reference is not the same as within the session due to the fact that the model comes from a deserialized json and then projected into entities.问题是对象引用与会话中的引用不同,因为模型来自反序列化的 json,然后投影到实体中。 Therefore i need to use Merge on the NHibernate session.因此我需要在 NHibernate 会话上使用 Merge。

Lets say we have this simplified model.假设我们有这个简化的模型。

class Product
{
    public int Id {get; protected set;}
    Public List<ProductVariant> ProductVariants {get; set;}
}
class ProductVariant
{
    public int Id {get; protected set;}
    //more stuff nested deeper
}

Saving of the object contains保存对象包含

Product -> id = 1
        -> ProductVariants -> ProductVariant -> id = 1
                           |> ProductVariant -> id = 2
                           |> ProductVariant -> id = 0 //Transient object

so:所以:

session.SaveOrUpdate(Product) //wont work, different object reference to the same identity
session.Merge(Product) //Wont work, there is a transient object

I knoew that i could proberbly just do something like:我知道我可以做一些类似的事情:

Foreach (productVariant : Product.ProductVariants){
    if( productVariant.Id == 0)
        sessiont.save(productVariant);
}
session.Merge(Product);

But isn't there a way to tell, Merge that if transient object just save ?但是有没有办法告诉,如果瞬态对象只是保存,则合并?

Cause lets say that the object that you would like to save is deeply nested, it would kinda suck to step through the entire nested object just to look for Ids that is 0因为假设您要保存的对象嵌套很深,单步遍历整个嵌套对象只是为了寻找 0 的 Id

Edit:编辑:

Looks like i could override the equals method to check if the Id is the same, and is of same type of object, but something in me says its a bad idea.看起来我可以重写 equals 方法来检查 Id 是否相同,并且是否属于相同类型的对象,但在我看来这是一个坏主意。 Even though set Id is protected.即使 set Id 受到保护。 However having overrriden the Equals methods means that i can use SaveOrUpdate, which is preferable.然而,重写 Equals 方法意味着我可以使用 SaveOrUpdate,这是更可取的。

like:喜欢:

class Product
{
    public int Id {get; protected set;}
    Public List<ProductVariant> ProductVariants {get; set;}

    public override bool Equals(Object obj) 
    {

       if (obj == null || GetType() != obj.GetType()) 
       return false;

       Product otherProduct = (Product)obj;
       return otherProduct.Id == Id
    }
}
class ProductVariant
{
    public int Id {get; protected set;}
    //more stuff nested deeper
    public override bool Equals(Object obj) 
    {

       if (obj == null || GetType() != obj.GetType()) 
       return false;

       ProductVariant otherProduct = (ProductVariant)obj;
       return otherProduct.Id == Id
    }
}

Atleast that what they say here: http://nhibernate.info/blog/2009/08/23/part-5-fixing-the-broken-stuff.html under problem #2至少他们在这里说的话:http: //nhibernate.info/blog/2009/08/23/part-5-fixing-the-broken-stuff.html在问题#2下

You need to cascade the ProductVariants list in the mapping of Product .您需要在Product的映射中级联ProductVariants列表。 NHibernate will adding the new ProductVariant if it is not persisted already.如果尚未持久化,NHibernate 将添加新的ProductVariant

If everything is set up correctly you don't need to call then any SaveOrUpdate() or Merge() method.如果一切都设置正确,则不需要调用任何SaveOrUpdate()Merge()方法。

If your still having problems with cascading there, and saving/updating a entity that has children entities created by your view and they have a id of 0, you will also get this message.如果您仍然存在级联问题,并且保存/更新具有由您的视图创建的子实体且它们的 id 为 0 的实体,您也会收到此消息。 So null our before saving/updating the child entity, if it's empty (not required or any input entered) and this will also resolve this problem.因此,如果它为空(不需要或输入任何输入),则在保存/更新子实体之前将我们的 null 设为空,这也将解决此问题。

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

相关问题 Nhibernate 保存对象的顺序 - Nhibernate order of saving objects NHibernate:保存瞬态实例时,身份ID如何更新? - NHibernate: How is identity Id updated when saving a transient instance? NHibernate错误消息:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - NHibernate Error Message: object references an unsaved transient instance - save the transient instance before flushing NHibernate 对象引用未保存的瞬态实例在刷新前保存瞬态实例 - NHibernate object references an unsaved transient instance save the transient instance before flushing NHibernate /多个会话和嵌套对象 - NHibernate / multiple sessions and nested objects 在NHibernate中将非持久对象添加并保存到持久对象 - Adding and saving not persistent objects to persistent object in NHibernate 在保存到Redis之前先压缩对象 - Compress objects before saving to redis 合并所有嵌套的Json对象 - Merge all Nested Json Objects 如何正确地使用瞬态对象更新NHibernate多对多关系? TransientObjectException - How do I properly update an NHibernate Many-to-Many relationship with Transient objects? TransientObjectException NHibernate:多对多关系无法首先保存子对象(或者:“无法插入null”或“临时对象”) - NHibernate: many-to-many relation fails to save child objects first (either: “cannot insert null” or: “transient object”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM