2010-06-09 2 views
0

동일한 항목의 하위를 참조하는 MyItem이라는 객체가 있습니다. 이 항목을 저장하기 위해 nhibernate 매핑 파일을 어떻게 설정합니까? 많은 자식과 많은 부모를 참조하는 객체의 nHibernate 저장소

public class MyItem 
{ 
    public virtual string Id {get;set;} 
    public virtual string Name {get;set;} 
    public virtual string Version {get;set;} 
    public virtual IList<MyItem> Children {get;set;}  
} 

그래서 대략 hbm.xml은 다음과 같습니다

<class name="MyItem" table="tb_myitem"> 
    <id name="Id" column="id" type="String" length="32"> 
    <generator class="uuid.hex" /> 
    </id> 
    <property name="Name"  column="name" /> 
    <property name="Version" column="version" /> 
    <bag name="Children" cascade="all-delete-orphan" lazy="false"> 
    <key column="children_id" /> 
    <one-to-many class="MyItem" not-found="ignore"/> 
    </bag>  
</class> 

이것은 내가 생각하지 않는다 작동하지 않을 것입니다. 아마도 다른 클래스를 만들고 MyItemChildren을 말하고 Children 멤버로 사용하고 그런 클래스에서 매핑을 수행해야할까요?

이것은 두 개의 테이블을 갖는 것을 의미합니다. 한 테이블에는 MyItem이 있고 다른 테이블에는 내 항목의 참조가 있습니다. 참고 : 하위 항목에는 많은 부모가있을 수 있습니다.

답변

0

나는 이것을 열심히 객체를로드하는 것으로 작업하고있다.

'MyItem'개체를로드 할 수 있으며 자식 (종속) 'MyItem'개체도로드 할 수 있습니다.

다음은

using Iesi.Collections.Generic; 

namespace Sample 
    { 
    public class MyItem 
     { 
     public virtual string Id { get; set; } 
     public virtual string Name { get; set; } 
     public virtual string Version { get; set; } 

     public virtual ISet<MyItem> Dependants { get; set; } 

     } 
    } 

부양 부모에 의존하는 아이들의 모음입니다 내 C# 클래스입니다. 각 MyItem에는 많은 MyItem 하위 항목이 있습니다. 이들은 IESI ISet <에 저장되어 있습니다.

최대 절전 모드 매핑 파일은 다음과 같습니다

<class name="Sample.MyItem, Sample" table="myitem"> 
     <id name="Id" column="id" type="String" length="32"> 
     <generator class="uuid.hex" /> 
     </id> 

     <property name="Name"  column="name"  type="String" length="80"/> 
     <property name="Version" column="version" type="String" length="25"/> 

     <set name="Dependants" table="myitemhierarchy" > 
     <key column="parentid" /> 
     <many-to-many class="Sample.MyItem, Sample" column="childid" /> 
     </set> 
    </class> 

이 문서는 대단히 도움 : http://web.archive.org/web/20090806071731/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/05/14/how-to-map-a-tree-in-nhibernate.aspx

관련 문제