Session.Clear() に問題があります。このステートメントは、UOW やキャッシュなどで行われたすべての変更を完全にリセットする必要があることを理解しています。問題はそうではないことです。私のシナリオは、以下のテストに示されています。アイテムに追加すると、それらの間に依存関係があり、他のアイテムが依存しているアイテムを削除しようとします。これにより、正しい例外が発生します。その後、セッションをクリアします。最後に、データベースに新しい項目を追加しようとします。NHibernate をフラッシュすると、失敗した削除ステートメントをもう一度実行しようとします。Session.Clear() の使用を誤解していますか? それとも、ここで何か他のものを見逃していますか?
[Fact]
public void Verify_Cant_Clear_Delete()
{
var session = SessionFactory.OpenSession();
var category = new ManufacturerCategory { Name = "category" };
var man = new Manufacturer { Category = category, Name = "man" };
session.Save(category);
session.Save(man);
session.Flush();
try
{
// this will cause
// NHibernate.Exceptions.GenericADOException: could not execute batch command.[SQL: SQL not available]
// ---> System.Data.SqlClient.SqlException: The DELETE statement conflicted with the REFERENCE constraint "ManufacturerCategoryId".
// The conflict occurred in database "LabelMaker-Tests", table "dbo.Manufacturers", column 'Category_id'.
session.Delete(category);
session.Flush();
}
catch (Exception ex)
{
// This should clear the session
session.Clear();
}
try
{
var category2 = new ManufacturerCategory { Name = "category 2" };
session.Save(category2);
session.Flush();
}
catch(Exception ex)
{
// this will cause ONCE AGAIN cause
// NHibernate.Exceptions.GenericADOException: could not execute batch command.[SQL: SQL not available]
// ---> System.Data.SqlClient.SqlException: The DELETE statement conflicted with the REFERENCE constraint "ManufacturerCategoryId".
// The conflict occurred in database "LabelMaker-Tests", table "dbo.Manufacturers", column 'Category_id'.
Assert.True(false);
}
}