2014-11-22 62 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