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();
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()
{
var expectedDocField = GetDocumentFieldDTOWithADO(DocumentFieldID);
expectedDocField.DocumentFieldOrgs = null;
Repository.Save(expectedDocField, false);
SessionFactory.GetCurrentSession().FlushAndEvict(expectedDocField);
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 . .