私のクラス Case には、Entity (クラス) をキーとして、Roles (列挙型) を値として持つ IDictionary があります。Case の新しいインスタンス (永続化されていない) を保存しようとすると、IDictionary が Entity の新しいインスタンスで満たされている場合、次のエラーが発生します。
NHibernate.TransientObjectException: オブジェクトが保存されていない一時インスタンスを参照しています - フラッシュする前に一時インスタンスを保存してください。タイプ: エンティティ
これらはクラスです (役割は列挙型です):
public class Case
{
public Case { EntityCollection = new Dictionary<Entity, Roles>(); }
public virtual int Id { get; set; }
public virtual IDictionary<Entity, Roles> EntityCollection { get; set; }
}
と
public class Entity
{
public virtual int Id { get; set; }
}
また、マッピングは次のとおりです。
<class name="Case" table="[Case]">
<id name="Id" column="Id" type="Int32" unsaved-value="any">
<generator class="hilo"/>
</id>
<map name="EntityCollection" table="CaseEntityRoles"
cascade="save-update" lazy="false" inverse="false">
<key column="CaseId" />
<index-many-to-many class="Entity"
column="EntityId" />
<element column="Roles" type="Roles" not-null="true" />
</map>
</class>
と
<class name="Entity" table="[Entity]">
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="hilo"/>
</id>
</class>
テストコードのサンプル:
[Test]
public void Can_add_new_case()
{
var newCase = new Case();
newCase.EntityCollection.Add(new Entity(), Roles.Role1);
newCase.EntityCollection.Add(new Entity(), Roles.Role2);
/* At which point I try to persist newCase and get an exception */
}
テストコードでは、newCase-instance は保持されますが、新しいエンティティは保持されません。version
エンティティに < > タグを追加したり、保存されていない値をいじったりするなど、さまざまなことを試しましたが、何も役に立たないようです。マッピングからわかるように、cascade="save-update" があります。何か案は?