2009-08-25 33 views
2

我有一個複合鍵映射到3個其他表的遺留表,因爲這個表中有其他屬性,因爲它不是一個簡單的映射表,我不能使用多一對多的解決方案來映射這個。nHibernate複合鍵類的類型不匹配

以下是我做了什麼:

<class name="classA" table="A"> 
<composite-id name="ID" class="AKey"> 
    <key-many-to-one name="Id_one" class="One" column="Id_one" /> 
    <key-many-to-one name="Id_two" class="Two" column="Id_two" /> 
    <key-many-to-one name="Id_three" class="Three" column="Id_three" /> 
</composite-id> 

AKEY僅僅是持有三個ID,並Id_one,Id_two和Id_three都被定義爲在各自的類int一個結構。

public struct Akey { 
    public int Id_one { get; set; } 
    public int Id_two { get; set; } 
    public int Id_three { get; set; } 
} 

編譯沒有問題,但是當我嘗試運行它,它給了我一個錯誤信息:

NHibernate.QueryException:在NHibernate.Criterion.SimpleExpression類型不匹配:ID預期型AKEY,實際類型System.Int32

請告訴我我做錯了或錯過了什麼。

非常感謝!

回答

5

如果你要使用鑰匙多到一個你會把類:

public class Akey { 
    public virtual One One {get; set;} 
    public virtual Two Two {get; set;} 
    public virtual Three Three {get; set;} 
} 

否則,如果你想要的ID您只需將它們映射爲CLASSA的屬性:

<composite-id> 
    <key-property name="Id_one" column="Id_one" /> 
    <key-property name="Id_two" column="Id_two" /> 
    <key-property name="Id_three" column="Id_three" /> 
</composite-id> 

public class classA { 
    public virtual int Id_one {get; set;} 
    public virtual int Id_two {get; set;} 
    public virtual int Id_three {get; set;} 

    // ... rest of props ... 
} 

或者像你這樣的組合有:

<composite-id name="ID" class="AKey"> 
    <key-property name="Id_one" column="Id_one" /> 
    <key-property name="Id_two" column="Id_two" /> 
    <key-property name="Id_three" column="Id_three" /> 
</composite-id> 

public class AKey { 
    public virtual int Id_one {get; set;} 
    public virtual int Id_two {get; set;} 
    public virtual int Id_three {get; set;} 
} 

public class classA { 
    public virtual AKey ID {get; set;} 

    // ... rest of props ... 
} 

最後...

<composite-id> 
    <key-many-to-one name="Id_one" class="One" column="Id_one" /> 
    <key-many-to-one name="Id_two" class="Two" column="Id_two" /> 
    <key-many-to-one name="Id_three" class="Three" column="Id_three" /> 
</composite-id> 

public class classA { 
    public virtual One One {get; set;} 
    public virtual Two Two {get; set;} 
    public virtual Three Three {get; set;} 

    // ... rest of props ... 
} 

去踢是否可以使用一個結構,因爲我不能勝任他們在C#中。

+0

感謝您的回覆!最後一部分幫助我解決了我的問題,現在我終於可以繼續前進! = P – Akey 2009-08-26 17:09:47

+0

耶......我寫完後意識到最後一個應該是第一個! – anonymous 2009-08-26 17:36:52