NHibernate Cascade Deletes

Let me start by displaying the mapping:

Parent:

<bag name="Communicatiekanalen" table="COMMUNICATIEKANAAL" inverse="true" cascade="delete" lazy="true" >
        <key column="SEK_PROFIEL"/>
        <one-to-many class="Crm.Hibernate.Communicatiekanaal,Crm.Hibernate" />
</bag>

Child:

<many-to-one name="SekProfiel" column="SEK_PROFIEL" class="Crm.Hibernate.Profiel,Crm.Hibernate" />

In other words: a profile can have many communication channels.

The following event is triggered in the user interface (user interface [ASP.NET Webforms]) (deleting a profile with communication channels connected to it):

    var profielDao = CrmConfiguration.GetDaoFactory().GetProfielDao();
    var profiel = profielDao.GetById(2194, true); //lets say '2194' is an ID that exists
    profielDao.Delete(profiel);

(DaoFactory is in the same project file, and the user interface is the ASP.NET website)

This code works.

IMPORTANT: The code uses the NHibernate 'open-session-in-view' template.

I have a service implementation that runs the same code (deleting a profile with communication channels). Some code ...

            var daof = CrmConfiguration.GetDaoFactory();
            CrmSettings.Instance.UserID = user;
            var profielDao = daof.GetProfielDao();

            profielDao.BeginTransaction();
            var profiel = profielDao.GetById(CrmEntitiesToHibernate.ParseStringToId(profileId), true);
            profielDao.Delete(profiel);
            profielDao.EndTransaction();

Where 'EndTransaction ()' executes 'commit'. I am testing this code with unit test ':

    [TestMethod]
    public void TestDeleteProfile()
    {
        //Getting a valid NEW profile
        var profile = GetSecundaryProfile();
        //Adding a communication channel to the profile
        CrmClient.AddCommunicationChannelForProfile(GetPlainCommunicationChannel(), profile.Id, CurrentUserId);
        //Calling the 'delete profile' method on the service --> FAIL - no cascade
        CrmClient.DeleteProfile(profile.Id, CurrentUserId);
    }

This code does not work. The following error calls me:

DELETE REFERENCE "R2_PROFIEL". "CRM_ontw", "dbo.COMMUNICATIEKANAAL", 'SEK_PROFIEL. .

, . UI, , . , ?


: :

    public void Delete(T entity)
    {
        try
        {
            OnDelete(entity);
        }
        catch (Exception)
        {
            SessionManager.Instance.RollbackTransactionOn(SessionFactoryConfigPath);
            throw;
        }
        session.Delete(entity);
    }

all-delete-orphan .

+3
2

. open-session-in-view NHibernate (, , ):

        finally
        {
            // No matter what happens, make sure all the sessions get closed
            foreach (SessionFactoryElement sessionFactorySettings in openSessionInViewSection.SessionFactories)
            {
                SessionManager.Instance.CloseSessionOn(sessionFactorySettings.FactoryConfigPath);
            }
        }

EndTransaction() .

, EndTransaction():

public void EndTransaction()
{
    try
    {
        SessionManager.Instance.CommitTransactionOn(SessionFactoryConfigPath);
    }
    finally
    {
        SessionManager.Instance.CloseSessionOn(SessionFactoryConfigPath);
    }
}
+2

cascade="delete" cascade="all-delete-orphan"

, , ISession. - , .Delete().

0

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


All Articles