私のクラスでは、Entity(クラス)をキーとし、Roles(enum)を値として持つIDictionaryを持っています。 IDictionaryを、エンティティの新しいインスタンスで満たされている場合の新しいインスタンスを(非永続)、保存しようとするとき、私は次のエラーを取得する:IDictionaryを保存するNHibernate:TransientObjectException
:NHibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing. Type: Entity
は、これらは、クラス(役割が列挙型である)されています
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 */
}
testcode newCaseインスタンスに永続が、新しいエンティティはありませんされています。私は< version
>タグをEntityに追加し、保存されていない値を混乱させるなど、さまざまなことを試しましたが、何も役立たないようです。マッピングからわかるように、私はカスケード= "save-update"を持っています。 アイデア
はい、これは私がやったこととほぼ同じです。 NHibernateが自動的に処理しないことは残念です。誰かが他の解決策を知っている場合(あるいはこのバグがバグであれば修正予定)は教えてください。しかしそれまでは、これを必要な解決策として受け入れる必要があります。 –