문제

그것은 매우 간단 될 수도 있지만 내가 나를 생각하는 뭔가가.나는 자 가입 테이블 Units, 모든 단위는 기본단위:

enter image description here

그래서 할 수 있는 쿼리를 위해 무언가가 다음과 같다:

enter image description here

테이블 units 매핑을 다음과 같은 클래스:

public class Units
{
    public virtual int Unit_Id { get; private set; }
    public virtual string Unit { get; set; }
    public virtual decimal Unit_Value { get; set; } 
    public virtual Units Main_Unit { get; set; }
}

니다.hbm 매핑 파일에 다음과 같다:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NewA.Domain"     namespace="NewA.Domain">
   <class name="NewA.Domain.Entities.Units,NewA.Domain" table="Units">
    <id name="Unit_Id" column="Unit_Id" type="Int32" length="4" unsaved-value="0"> 
      <generator class="native">
      </generator>
    </id>
    <property name="Unit" column="Unit" type="string" length="50" not-null="true"/>
    <one-to-one name="Main_Unit" class="NewA.Domain.Entities.Units,NewA.Domain"/>
  </class>
</hibernate-mapping>

테스트할 때 이러한 entity 다음과 같은 코드:

Units unit = _UnitsRepository.GetById(2);
string parent_unitname = unit.Main_Unit.Unit;
Assert.AreEqual("pack",parent_unitname);

나는 다음과 같은 excpetion:

Expected values to be equal.

Expected Value : "pack"
Actual Value   : "kg"

문제는 Main_UnitUnit 엔터티를 참조 자체가,그래서 내가 여기 없어??, 할 수 있는 방법은 다음과 같이 쓸 재귀 SQL CTE 을 적용 순위에 있기 때문에 동일한 문제는 다른 더 복잡한 자기 가입 테이블과 함께 더 복잡한 쿼리를 처리합니다.

도움이 되었습니까?

해결책

대신에 많은 매핑이 필요합니다. 이 구성을 시도하십시오.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="NewA.Domain.Entities.Units.Units, NewA.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Units`">
    <id name="Unit_Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Unit_Id" />
      <generator class="native" />
    </id>
    <property name="Unit" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Unit" />
    </property>
    <property name="Unit_Value" type="System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Unit_Value" />
    </property>
    <many-to-one class="NewA.Domain.Entities.Units.Units, NewA.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Main_Unit">
      <column name="Main_Unit_id" />
    </many-to-one>
  </class>
</hibernate-mapping>
.

나는 유창한 nhibernate 을 시도해보십시오 - 당신을 위해 매핑을 동적으로 생성 할 수 있습니다. 내가 사용한 구성은 다음과 같습니다.

var fluent = Fluently.Configure()
    .Mappings(c => c.AutoMappings.Add(AutoMap.AssemblyOf<Units>()
        .Override<Units>(u => u.Id(uu => uu.Unit_Id).GeneratedBy.Native())))
    .Database(() => SQLiteConfiguration.Standard.UsingFile("test.sqlite3"));

var configuration = fluent.BuildConfiguration();

// Generate database schema
new SchemaExport(configuration).Create(false, true);

var sessionFactory = configuration.BuildSessionFactory();
// Now just open session and do whatever you need
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top