NHibernate Cascade Collection Deletes When Inserting a New Element, so the Non-Empty Collection

I have the following Fluent Mappings:

    public ScanDeliverySessionMap()
    {
        Id(x => x.Id);
        ...
        ...
        HasManyToMany(x => x.ToScanForms) <--- IList<Form> ToScanForms --->
            .Table("ToScanForm")
            .ParentKeyColumn("SessionId")
            .ChildKeyColumn("FormId").Cascade.SaveUpdate();
    }

    public FormMap()
    {

        Id(x => x.Id).Column("FormID").GeneratedBy.Foreign("Log");

        ....
        ....

        HasManyToMany(x => x.ScanDeliverySessions)
            .Table("ToScanForm")
            .ParentKeyColumn("FormId")
            .ChildKeyColumn("SessionId").Inverse();
    }

When I try to insert a new form into the ToScanForms collection Everything seems to work correctly, but look at NHProf I see that NH casacde DELETE on top of all ToScanForms elements and then NH INSERT for ToScanForms elements, including the new element.

Some screenshots: alt textalt text

+3
source share
1 answer

This is because nhibernate does not know which objects in the collection are new and old, so it must delete everything and then reinsert them.

, : ICollection HasManyToMany . :

public ScanDeliverySessionMap()
{
    Id(x => x.Id);
    ...
    ...
    HasManyToMany(x => x.ToScanForms) //<--- ICollection<Form> ToScanForms --->
        .AsSet()
        .Table("ToScanForm")
        .ParentKeyColumn("SessionId")
        .ChildKeyColumn("FormId").Cascade.SaveUpdate();
}

public FormMap()
{

    Id(x => x.Id).Column("FormID").GeneratedBy.Foreign("Log");

    ....
    ....

    HasManyToMany(x => x.ScanDeliverySessions)
        .AsSet()
        .Table("ToScanForm")
        .ParentKeyColumn("FormId")
        .ChildKeyColumn("SessionId").Inverse();
}

nhibernate HashSet Iesi Collections, , .

+4

Source: https://habr.com/ru/post/1772619/


All Articles