首先是映射,
<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>
一次通过测试,
[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);
}
我的问题出现在我的应用程序的控制器中,
[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.");
});
}
根据 Log4Net,没有发布任何删除。正如我所说,上面的测试通过了,所以映射应该是正确的。
有什么线索吗?