次のコードは、Order.OrderItems プロパティ (IList) がコミットされている場合に StaleStateException 例外をスローします。例外の全文は次のとおりです。
タイプ 'NHibernate.StaleStateException' の未処理の例外が NHibernate.dll で発生しました
追加情報: 予期しない行数: 0; 予想: 1
私は NHibernate を使い始めたばかりですが、これは私にとってまったく意味がありません。誰が何が悪いのか説明できますか?
ほとんどのコードは以下に追加されています。申し訳ありませんが、重要なことを省略するよりはましだと思いました。
OrderItems = orderItemsという行をコメントアウトすると、他のすべてが正常に機能します。
using System;
using System.Collections.Generic;
using System.IO;
using AutomappingSample.Domain;
using FluentNHibernate.AutoMap;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace AutomappingSample
{
class Program
{
private const string DbFile = "AutomappingSample.db";
private const bool useSqlServerCe = true;
static void Main()
{
var factory = CreateSessionFactory();
using (var session = factory.OpenSession())
using(var tx = session.BeginTransaction())
{
var product1 = new Product
{
Name = "Apples",
UnitPrice = 4.5m,
Discontinued = true
};
var product2 = new Product
{
Name = "Pears",
UnitPrice = 3.5m,
Discontinued = false
};
session.Save(product1);
session.Save(product2);
var customer = new Customer
{
FirstName = "John",
LastName = "Doe",
};
session.Save(customer);
var orderItems = new List<OrderItem>
{
new OrderItem {Id = 1, Quantity = 100, Product = product1},
new OrderItem {Id = 2, Quantity = 200, Product = product2},
};
var order = new Order()
{
Customer = customer,
Id = 1,
OrderDate = DateTime.Now,
// CAUSES FOLLOWING EXCEPTION WHEN COMMIT:
// An unhandled exception of type 'NHibernate.StaleStateException'
// occurred in NHibernate.dll
//
// Additional information: Unexpected row count: 0; expected: 1
OrderItems = orderItems
};
session.Save(order);
// EXCEPTION IS THROWN HERE
tx.Commit();
}
Console.WriteLine("Hit enter to exit...");
Console.ReadLine();
}
private static ISessionFactory CreateSessionFactory()
{
IPersistenceConfigurer persistenceConfigurer;
if (useSqlServerCe)
persistenceConfigurer =
MsSqlCeConfiguration.Standard.ConnectionString(c => c.Is("Data Source=AutomappingSample.sdf"));
else
persistenceConfigurer = SQLiteConfiguration.Standard.UsingFile(DbFile);
return Fluently.Configure()
.Database(persistenceConfigurer)
.Mappings(m => m.AutoMappings.Add(
AutoPersistenceModel
.MapEntitiesFromAssemblyOf<Customer>()
.WithSetup(s => { s.IsBaseType = type => type == typeof (EntityBase); })
.Where(t => t.Namespace.EndsWith("Domain"))
.ConventionDiscovery.Add<MyStringLengthConvention>()
.ConventionDiscovery.Add<MyIdConvention>()
.ConventionDiscovery.Add<MyForeignKeyConvention>()
))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
private static void BuildSchema(Configuration config)
{
// delete the existing db on each run (only for SQLite)
if (File.Exists(DbFile))
File.Delete(DbFile);
// this NHibernate tool takes a configuration (with mapping info in)
// and exports a database schema from it
new SchemaExport(config)
.Create(true, true);
}
}
}
namespace AutomappingSample.Domain
{
public class EntityBase
{
public virtual int Id { get; set; }
}
}
using System;
using System.Collections.Generic;
namespace AutomappingSample.Domain
{
public class Order : EntityBase
{
public virtual DateTime OrderDate { get; set; }
public virtual Customer Customer { get; set; }
public virtual IList<OrderItem> OrderItems { get; set; }
}
}
namespace AutomappingSample.Domain
{
public class OrderItem : EntityBase
{
public virtual int Quantity { get; set; }
public virtual Product Product { get; set; }
}
}