Nhibernate will not remove a child from the collection

Mapping first,

<set name="Comments" table="cm_events_venue_comment" inverse="true"
        lazy="true" generic="true" cascade="all-delete-orphan"
        batch-size="10" order-by="dateCreated ASC">
    <cache usage="read-write" />
    <key column="venueId" />
    <one-to-many class="VenueComment" />
</set>

Passing test

[Test]
public void CanSaveAndDeleteComments()
{
    User u = TestDataHelper.CreateUser("Sir", "Talkalot");
    VenueComment comment;
    Venue v = GetVenueById();

    PerformInTransaction(() =>
    {
        userRepository.SaveOrUpdate(u);
        comment = new VenueComment()
        {
            Commenter = u,
            Text = "I like ifs and buts and i cannot lie..",
            DateCreated = DateTime.Now
        };
        v.AddComment(comment);
        comment = new VenueComment()
        {
            Commenter = u,
            Text = "And words ending in 'ly'",
            DateCreated = DateTime.Now
        };
        v.AddComment(comment);
        Assert.DoesNotThrow(()=>venueRepository.SaveOrUpdate(v));
    });

    // Test retrieval 
    Session.Evict(v);

    v = GetVenueById();
    v.Comments.Count.ShouldEqual(2);
    var c = v.Comments.FirstOrDefault<VenueComment>();
    c.Commenter.Id.ShouldEqual(u.Id);

    // and deletion
    PerformInTransaction(() =>
    {
        v.RemoveComment(c);
    });

    Session.Evict(v);
    v = GetVenueById();
    v.Comments.Count.ShouldEqual(1);

}

My problems arise in the controller of my application,

[Authenticate, AcceptAjax]
public ActionResult DeleteComment(int id)
{
    return PerformInTransaction<ActionResult>(() =>
        {
            var comment = commonDao.GetObjectById<VenueComment>(id);
            if (comment == null)
                return JsonFailure("Comment not found");

            if (comment.Commenter.Id == CurrentUser.Id ||
                AuthUtil.IsAdministrator()) //
            {

               var venue = comment.Venue;
               venue.RemoveComment(comment);

                return JsonSuccess(id);
            }
            return JsonFailure("You can only delete comments you created.");
        });
}

According to Log4Net, there were no deletions. As I said, the above test passes, so the display should be correct.

Any clues?

+3
source share
3 answers

inverse , , inverse="false" NHibernate (, ) / / -.

, inverse="true", NHibernate reference . , parent-reference, NHibernate .

, inverse="true" .

:

RemoveComment (, child.Parent = null). Session, inverse="true", , , , , NHibernate a delete, NHibernate delete, - a) (inverse="false"), , , ) Session.

+4

, , . .

+1

The mystery decided .... It was a case of a merger of two codebases.

One older dao (commonDao) had an error in which the Session internal property created a new session from the factory session to access the resource, so a different session was held between loading the object and flush ().

The newer code uses repositories based on the S # arp Architecture, which has automatic session management, so the problem does not appear in unit tests.

+1
source

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


All Articles