2013-06-24 61 views
0

我想將我的FluentNHibernate映射轉換爲使用NHibernate 3.3.3 NHibernate映射的代碼。目標是升級到NHibernate 3.3.3並減少正在分發的程序集數量。我在將FluentNHibernate的引用映射轉換爲多對一映射時遇到了一些麻煩。NHibernate映射代碼ManyToOne與CompositeIdentity

我的許多實體都有需要翻譯的描述。爲此,我使用包含所有可用語言中的這些文本的文本表格。我使用文本ID來引用文本表,然後在數據訪問對象I中過濾所需的語言。這工作創建使用NHibernate 3.1和FluentNHibernate,與NHibernate 3.3.3和映射的代碼,但我只是一個MappingException說:屬性映射具有錯誤的列數:Category.Description類型:文本。

我的新映射在哪裏出錯?或者這種類型的映射在NHibernate 3.3.3中不可行。

這是文本表(SQL-server 2008)。

CREATE TABLE Texts (
    ID   int   NOT NULL, 
    languageID nvarchar(10) NOT NULL, 
    Singular nvarchar(max) NOT NULL, 
    Plural  nvarchar(max) NULL, 
CONSTRAINT PK_Texts PRIMARY KEY CLUSTERED (ID ASC, languageID ASC) 
WITH (PAD_INDEX    = OFF, 
     STATISTICS_NORECOMPUTE = OFF, 
     IGNORE_DUP_KEY   = OFF, 
     ALLOW_ROW_LOCKS  = ON, 
     ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]) ON [PRIMARY] 

文本類:

public class Text 
{ 
    public Text(int id, string language, string singular, string plural) 
    { 
     this.ID = new TextCompositeID(id, language); 
     this.Singular = singular; 
     this.Plural = plural; 
    } 
    public TextCompositeID ID { get; private set; } 
    public string Plural { get; private set; } 
    public string Singular { get; set; } 
    public override bool Equals(object obj) 
    { 
     var text = (Text)obj; 
     if (text == null) 
     { 
      return false; 
     } 
     return this.ID.Equals(text.ID); 
    } 
    public override int GetHashCode() 
    { 
     return this.ID.GetHashCode(); 
    } 
} 

此處作爲一例是Category類:

public class Category 
{ 
    public int ID { get; set; } 
    public Text Description { get; set; } 
} 

的範疇類FluentNHibernate XML映射看起來是這樣的:

<class xmlns="urn:nhibernate-mapping-2.2" 
     mutable="true" 
     name="Category" 
     lazy="false" 
     table="Category" 
     where="IsObsolete=0"> 
    <id name="ID" type="System.Int32"> 
     <column name="ID" not-null="true" /> 
     <generator class="native" /> 
    </id> 
    <many-to-one cascade="none" 
       class="Text" 
       name="Description"> 
     <column name="TextID" 
       not-null="true" 
       unique="false" /> 
    </many-to-one> 
</class> 

這是g從這個enerated:

public class CategoryMap : ClassMap<Category> 
{ 
    public CategoryMap() 
    { 
     this.Table("Category"); 
     Not.LazyLoad(); 
     this.Where("IsObsolete=0"); 
     Id(x => x.ID) 
      .Column("ID") 
      .GeneratedBy.Native() 
      .Not.Nullable(); 
     References(x => x.Description) 
      .Column("DescriptionID") 
      .Cascade.None() 
      .Not.Unique() 
      .Not.Nullable(); 
    } 
} 

這是NHibernate的ClassMapping我創建:

public CategoryMap() 
{ 
    this.Lazy(false); 
    this.Mutable(true); 
    this.Table("Category"); 
    this.Where("IsObsolete=0"); 
    this.Id(
     x => x.ID, 
     map => 
     { 
      map.Column("ID"); 
      map.Generator(Generators.Native); 
     }); 
    this.ManyToOne(
     x => x.Description, 
     map => 
     { 
      map.Cascade(Cascade.None); 
      map.Class(typeof(Text)); 
      map.Column("TextID"); 
      map.Fetch(FetchKind.Join); 
      map.Lazy(LazyRelation.NoLazy); 
      map.ForeignKey("none"); 
     }); 
} 

由此我得到這個XML映射:

<class name="Category" 
     lazy="false" 
     table="Category" 
     where="IsObsolete=0"> 
    <id name="ID" 
     column="ID" 
     type="Int32"> 
     <generator class="native" /> 
    </id> 
    <many-to-one name="Description" 
       class="Text" 
       column="TextID" 
       fetch="join" 
       foreign-key="none" 
       lazy="false" /> 
</class> 

回答

1

我找到了答案我自己。我不得不從文字類中刪除複合ID:

public class Text 
{ 
    public Text(int id, string language, string singular, string plural) 
    { 
     this.ID = id; 
     this.LanguageID = language; 
     this.Singular = singular; 
     this.Plural = plural; 
    } 
    public int ID { get; private set; } 
    public string LanguageID { get; private set; } 
    public string Plural { get; private set; } 
    public string Singular { get; set; } 
    public override bool Equals(object obj) 
    { 
     var text = (Text)obj; 
     if (text == null) 
     { 
      return false; 
     } 
     return this.ID.Equals(text.ID); 
    } 
    public override int GetHashCode() 
    { 
     return this.ID.GetHashCode(); 
    } 
} 

的類別映射已經成爲:

public CategoryMap() 
{ 
    this.Lazy(false); 
    this.Mutable(true); 
    this.Table("Category"); 
    this.Where("IsObsolete=0"); 
    this.Id(
     x => x.ID, 
     map => 
     { 
      map.Column("ID"); 
      map.Generator(Generators.Native); 
     }); 
    this.ManyToOne(
     x => x.Description, 
     map => 
     { 
      map.Column("TextID"); 
      map.Fetch(FetchKind.Join); 
      map.ForeignKey("none"); 
      map.Lazy(LazyRelation.NoLazy); 
     }); 
} 

在數據訪問對象老QueryOver查詢現在得到我所要的結果。