NHibernate mapping by code (Loquacious) - Cascade options
Asked Answered
F

2

12

I have a question on Cascade enum options behavior when using NHibernate Mapping By Code.

Enum has following options:

[Flags]
public enum Cascade
{
    None = 0,
    Persist = 2,
    Refresh = 4,
    Merge = 8,
    Remove = 16,
    Detach = 32,
    ReAttach = 64,
    DeleteOrphans = 128,
    All = 256,
}

They are intended to be used like bit flag combinations (as far as I get it).

I've looked thru NHibernate documentation, and the following cascade options for XML mappings are defined there: Lifecycles and object graphs

Can anyone describe cascade options from new Nhibernate mapping by code? Half of them are self describing, other half is not.

Farm answered 21/1, 2013 at 12:25 Comment(3)
which are not self describing?Rearward
I'm not sure. Persist = Save-Update, Refresh? Merge? Remove=Delete, Detach=? ReAttach=? DeleteOrphans=delete-orphan, All=all.Farm
I agree, this enum is a mess, All should really include all of the bitwise flags...Gothard
A
15

From src\NHibernate\Mapping\ByCode\Impl\CascadeConverter.cs

    private static IEnumerable<string> CascadeDefinitions(this Cascade source)
    {
        if (source.Has(Cascade.All))
        {
            yield return "all";
        }
        if (source.Has(Cascade.Persist))
        {
            yield return "save-update, persist";
        }
        if (source.Has(Cascade.Refresh))
        {
            yield return "refresh";
        }
        if (source.Has(Cascade.Merge))
        {
            yield return "merge";
        }
        if (source.Has(Cascade.Remove))
        {
            yield return "delete";
        }
        if (source.Has(Cascade.Detach))
        {
            yield return "evict";
        }
        if (source.Has(Cascade.ReAttach))
        {
            yield return "lock";
        }
        if (source.Has(Cascade.DeleteOrphans))
        {
            yield return "delete-orphan";
        }
    }

Note: all cascades all except of delete-orphan.

Anima answered 22/1, 2013 at 13:55 Comment(0)
R
7
  • None: nothing cascades
  • Persist = ISession.SaveOrUpdate
  • Refresh = ISession.Refresh: loads the db state of the object into memory and updates its properties
  • Merge = ISession.Merge: loads the object with the same Id from db and updates its properties with the properties of the given instance. returns the loaded object
  • Remove = ISession.Delete: delete the given instance in db and detach from session
  • Detach = ISession.Evict: removes the object from the session/change tracking
  • ReAttach = ISession.Lock(LockMode.None): reattach the given unmodified instance with the session
  • DeleteOrphans: delete associated objects which are not referenced by the parent
  • All: all of the obove except DeleteOrphans (thx to @Stefan Steinegger)
Rearward answered 22/1, 2013 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.