2014-11-22 63 views
0

我試圖加入別名多次,但它取決於它應該這樣做的次數。重命名NHibernate標準

Employee employeeAlias = null; 
Thing thingAlias = null; 
var names = string[] {"A", "B", "C", "D", "E"} // number of values could vary 
for(int i = 0; i < names.Length ; i++) 
{ 
    queryOver 
     .JoinAlias(() => employeeAlias.Things, 
       () => thingAlias, 
        JoinType.LeftOuterJoin, 
        Restrictions.Eq(Projections.Property(() => thingAlias.Name, 
            names[i])); 
} 

然而,在執行時,NHibernate的拋出一個錯誤說:

NHibernate.QueryException: duplicate alias: thingAlias ---> System.ArgumentException: An item with the same key has already been added

有沒有一種辦法可以讓NHibernate的加入一個別名與指定的名稱,以便它不會重新使用變量的名字?

謝謝你們。這是我昨天以來的問題,我找不到合適的解決方案。

PS:

我試圖做這樣的事情:

SELECT * FROM Employee e 
LEFT OUTER JOIN Thing t on e.Id = t.EmployeedId AND t.Name = "A" 
LEFT OUTER JOIN Thing t on e.Id = t.EmployeedId AND t.Name = "B" 

回答

1

什麼你正在努力實現是不可能的。每個關係(many-to-one,one-to-many)只能使用一次。

爲了支持這一說法,請點擊此處查看:CriteriaQueryTranslator.cs

private void CreateAssociationPathCriteriaMap() 
{ 
    foreach (CriteriaImpl.Subcriteria crit in rootCriteria.IterateSubcriteria()) 
    { 
     string wholeAssociationPath = GetWholeAssociationPath(crit); 
     try 
     { 
      associationPathCriteriaMap.Add(wholeAssociationPath, crit); 
     } 
     catch (ArgumentException ae) 
     { 
      throw new QueryException("duplicate association path: " 
            + wholeAssociationPath, ae); 
     } 

所以,我們可以在這裏看到,無論別名,在年底前,所有associtated關係(連接)加入到associationPathCriteriaMap,這是IDictionary<string, ICriteria>

這意味着,現在有辦法如何在兩種遊戲相同的鍵...

+0

請看看我的最後一次編輯..你對我如何能做到這一點在任何NHibernate的想法?之前我問過我的問題的原因是因爲我認爲這是實現我所需的SQL腳本的正確方法。 – 2014-11-22 09:21:34

+0

我想說,唯一的方法就是將更多的集合映射到同一個表。你需要使用WHERE來過濾它們。我的意思是有事情1,事情2,事情3,然後你可以根據需要加入他們。檢查http://nhforge.org/doc/nh/en/index.html#collections-mapping屬性'哪裏' – 2014-11-22 10:13:22

+0

,但這是否意味着我會硬編碼嗎? 連接數是動態的。不能硬編碼的東西1,東西2,東西3 .. :( – 2014-11-22 10:18:56

0

我沒有試過,但我想用for-loopRestrictions.Eq的相反,我們可以(不知道,但如果你沒試過)使用Restrictions.In喜歡 -

Restrictions.In(Projections.Property(() => thingAlias.Name, names)); 
+0

沒有。我想投影成一列每個名字:( – 2014-11-24 15:10:39