필자는 클래스에 Entity (클래스)를 키로, 역할 (열거 형)을 값으로 갖는다. (열거를 역할이다)이이 클래스IDictionary를 저장하는 NHibernate : TransientObjectException
NHibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing. Type: Entity
은 다음과 같습니다 : 새로운 인스턴스를 저장하려고하면 IDictionary가 나는 다음과 같은 오류가 엔터티의 새로운 인스턴스로 가득 케이스,의 (비 지속)
다음과 같이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>
샘플 다음 testcode에서
[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 인스턴스가 지속되고 있지만, 새로운 개체가 없습니다. 나는 < version
태그를 Entity에 추가하고 저장되지 않은 값으로 어지럽히는 것과 같은 많은 다른 작업을 시도했지만 아무것도 도움이되지 않습니다. 맵핑에서 알 수 있듯이 계단식 = "save-update"가 있습니다. 아이디어가 있으십니까?
네, 제가이 일을 끝내기까지 한 것입니다. NHibernate에 의해 자동으로 처리되지 않는 것은 부끄러운 일입니다. 다른 솔루션을 아는 사람이라면 (또는이 버그가 언제 버그인지 알려면 수정 될 것입니다.) 알려주세요. 그러나 그 때까지는 이것을 필요한 해결책으로 받아 들여야합니다. –