Is it possible to use Cascade.All () EXCEPT Delete?

I get the list of objects separately (and not from NHibernate) and set the parent IEnumerable objects to be equal to this returned object. Initially, we only needed to read objects. Then we needed to update only certain fields of the parent. Recently, we needed to update fields for a child. So far, everything has been fine with SaveOrUpdate (). Now I need to update the children, even if the collection of children was attached to an isolated parent (without using NHibernate). The following code updates the parent, but not the children. If I do everything, the children will be deleted if the parent does not have a collection. I do not want to do this because I am worried about using this method that does not take this behavior into account.

DESIRED BEHAVIOR:
 1. Cascade any changes to the collection (regardless of whether NHibernate source code was received or not). 2. Do not delete objects, even if the parent does not have a collection.

Is it possible?

This is our NHibernate save method:

[Transaction]
public int? Save(DocumentFieldDTO entity, bool autoFlush)
{
    var persisted = CurrentSession.Merge(entity);

    entity.DocumentFieldID = persisted.DocumentFieldID;
    if (autoFlush) { CurrentSession.Flush(); }
    return entity.DocumentFieldID;
}

DocumentFieldDTOMap looked like this:

public class DocumentFieldDTOMap : EntityMapBase
{

    public DocumentFieldDTOMap()
    {    
        Table("DocumentField");

        Id(m => m.DocumentFieldID).GeneratedBy.Increment().UnsavedValue(null);

        Map(x => x.Name);

        Map(x => x.DocumentSectionID).Not.Update();
        // .... Lots of other fields ....//

        HasMany(x => x.DocumentFieldOrgs)
        .Cascade.SaveUpdate()
        .LazyLoad()
        .KeyColumn("DocumentFieldID");
        }
    }

}

If I change Cascade.SaveUpdate()to Cascade.All(), the update will work, but will also be deleted. I want to exclude the possibility of deletion.

UPDATE (1/27/2014):

I simply confirmed that the deletion was cascading when the display was SaveUpdate(), so this is not so important, since I am not modifying the existing functionality. Anyway, I would like to update all cascading EXCEPT delete. A solution, if possible, would be useful for future references.

UPDATE (2/10/2014)

, , , "SaveUpdate()". GetDocumentFieldDTOWithADO(DocumentFieldID) , NHibernate, 318 DocumentFieldOrgs ( ) 0 . , ? , Merge?

    [Test]
    public void Save_ShouldDeleteDocumentFieldOrgs_WhenSavingDocumentFieldWithoutDocFieldOrgsList()
    {
        //arrange
        var expectedDocField = GetDocumentFieldDTOWithADO(DocumentFieldID);
        expectedDocField.DocumentFieldOrgs = null;

        //act
        Repository.Save(expectedDocField, false);
        SessionFactory.GetCurrentSession().FlushAndEvict(expectedDocField);

        //assert
        var actualDocField = GetDocumentFieldDTOWithADO(DocumentFieldID);

        actualDocField.DocumentFieldOrgs.Should()
            .BeEmpty("DocumentFieldOrgs should be deleted if the parent does not have a child collection");        
    }

(2/11/2014) - . NHibernate . .

+4
1

UPDATE,

, , :

parent.Children null , - .

, , - (, Parent Children, )

1) cascade="save-update"
NHibernate, Save() Update()

2) parent

var session = ... // get a ISession for our test
var parent = session.Get<Parent>(1); // e.g. DocumentFieldDTO 

// NOT Empty -- is true:  IsNotEmpty()
Assert.IsTrue(parent.Children.IsNotEmpty()); // e.g. DocumentFieldOrgs

3) , NHibernate :

parent.Children = null;

session.Flush();
session.Clear();

SQL-:

exec sp_executesql N'UPDATE [schema].[Child_Table] 
      SET ParentId = null WHERE ParentId = @p0',N'@p0 int',@p0=1

, - save-update NHibernate , . UPDATing

4) Parent

parent = session.Get<Parent>(1);

// EMPTY -- is true: IsEmpty()
Assert.IsTrue(parent.Children.IsEmpty());

: , NHibernate , . . ,

: public int? Save(...). NHibernate , , , Ayende, NHibernate Cascades: , -- -

:

:
1) (, NHibernate ).
2) , .

, Cascade concept . :

,
  //
  / ()

NHiberante : 9.9. Lifecyles ()

... cascade="all" / , // // (ren).... , , , <one-to-many>, cascade = "all-delete-orphan"...

. , .

:

Save(), :

  • "" - - . , session.Flush(). , ISession, .
+1

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


All Articles