1
我有一個類「實驗室」,它代表了一個使用我的程序的公司部門。它有一個名爲「員工」的子集,旨在包含屬於該實驗室的所有員工。 employee表中包含一個名爲「Department」的字符串字段,它代表了他們屬於哪個部門,Lab表中包含一個名爲「Department」的字符串字段,表示實驗室是哪個部門。員工部門與實驗室部門相匹配時,員工屬於實驗室。NHibernate與字符串映射,沒有真正的外鍵
我意識到僱員表中包含一個LabID字段將是更好的方法,但員工表不在我的控制之下,我不想修改它。我寧願在不添加員工到實驗室的映射表的情況下這樣做,因爲針對部門的部門提供了相同的結果。
這是我試圖使用的縮略映射。
<class name="Lab">
<id name="LabID">
<generator class="native" />
</id>
<property name="Department" />
<!--- other properties --->
<set name="Employees" table="Employee_copy" generic="true" inverse="true">
<key column="Department" property-ref="Department" />
<one-to-many class="Employee" />
</set>
</class>
返回該錯誤是
Identifier type mismatch; Found:<System.Int32> Expected:<System.String> Parameter name: identifier
生成的查詢運行正常。
SELECT employees0_.Department as Department1_, employees0_.EmployeeID as EmployeeID1_, employees0_.EmployeeID as EmployeeID30_0_, employees0_.CommonName as CommonName30_0_, employees0_.LastName as LastName30_0_, employees0_.NetUserID as NetUserID30_0_, employees0_.CompanyID as CompanyID30_0_, employees0_.Department as Department30_0_ FROM Employee employees0_ WHERE employees0_.Department='IS'
所以我做錯了什麼?
編輯:
請注意反向關聯正常工作。
<many-to-one name="Lab" column="Department" property-ref="Department" class="Lab" />
兩個對象 - 實驗室的相關屬性:
public virtual int LabID { get; set; }
public virtual string Department { get; set; }
public virtual ISet<Employee> Employees { get; set; }
員工:
public virtual int EmployeeID { get; protected set; }
public virtual string Department { get; protected set; }
public virtual Lab Lab { get; protected set; }