NHibernate.QueryException:重复的关联路径

发布于 2024-12-13 07:19:49 字数 1355 浏览 0 评论 0原文

下面的代码片段描述了我想使用查询执行的操作,但它因上述错误而崩溃。

建筑物和 AttributeValues 之间存在一对多关系,我的目标是找到 AttributeValue“Large”AttributeValue 的所有建筑物> “蓝色”

        var attributeValueAlias1 = new AttributeValue();
        var attributeValueAlias2 = new AttributeValue();

        var result = repository
            .CreateCriteriaFor<Buildings>()
            .Join(o=>o.AttributeValues, ()=> attributeValueAlias1)
            .Join(o=>o.AttributeValues, ()=> attributeValueAlias2)
            .Add(() => attributeValueAlias1.Value == "Large")
            .Add(() => attributeValueAlias2.Value == "Blue")
            .List();

那里的帖子很少,但没有一个解决此处描述的问题和 这里

我可以在添加别名之前使用 criteria.GetCriteriaByAlias(alias) 添加成员资格测试,这样就不会发生重复别名错误,但只有一个连接,它会导致 SQL where 子句永远不会成立

SELECT *
FROM Buildings  this_
    inner join AttributeValues attributev1_
    on this_.Id = attributev1_.BuildingId
WHERE 
    attributev1_.Value = 'Large'
    and attributev1_.Value = 'Blue'

这不是一个复杂或不寻常的查询,所以如果没有解决办法,我会感到惊讶。

The code fragment below describes what I want to do using queries but it blows up with the above error.

There is a 1 to many relationship between Buildings and AttributeValues and my aim is to find all Buildings which have an AttributeValue of "Large" and an AttributeValue of "Blue".

        var attributeValueAlias1 = new AttributeValue();
        var attributeValueAlias2 = new AttributeValue();

        var result = repository
            .CreateCriteriaFor<Buildings>()
            .Join(o=>o.AttributeValues, ()=> attributeValueAlias1)
            .Join(o=>o.AttributeValues, ()=> attributeValueAlias2)
            .Add(() => attributeValueAlias1.Value == "Large")
            .Add(() => attributeValueAlias2.Value == "Blue")
            .List();

There are few posts out there but none with a solution to the problem described here and here.

I can make it so a membership test is added using criteria.GetCriteriaByAlias(alias) before adding the alias so the Duplicate alias error does not occur but then there is only a single join and it results in a SQL where clause which will never be true

SELECT *
FROM Buildings  this_
    inner join AttributeValues attributev1_
    on this_.Id = attributev1_.BuildingId
WHERE 
    attributev1_.Value = 'Large'
    and attributev1_.Value = 'Blue'

It is not a complicated or unusual query so I'd be surprised if there isn't a work around.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相守太难 2024-12-20 07:19:49

您不应该使用联接来执行此操作,正如您可能从异常中猜到的那样。您应该使用两个子查询来测试值“Blue”和“Large”是否在集合中。我不太擅长 ICriteria,但生成的 SQL 应该如下所示,并且我认为这在 ICriteria 中是可能的(使用 Subqueries.PropertyIn)。

select * from Buildings b
inner join AttributeValues av on b.Id = av.BuildingId
where av.Id in (select Id from AttributeValues av2 where av2.BuildingId=b.BuildingId and  av2.Value="Large")
and av.Id in (select Id from AttributeValues av3 where av3.BuildingId=b.BuildingId and av3.Value="Blue")

You shouldn't use a join to do this, as you probably guessed from the exception. You should use two subqueries to test if the values "Blue" and "Large" are in the collection. I'm not great at ICriteria, but the resulting SQL should look like this and I think this is possible in ICriteria (using Subqueries.PropertyIn).

select * from Buildings b
inner join AttributeValues av on b.Id = av.BuildingId
where av.Id in (select Id from AttributeValues av2 where av2.BuildingId=b.BuildingId and  av2.Value="Large")
and av.Id in (select Id from AttributeValues av3 where av3.BuildingId=b.BuildingId and av3.Value="Blue")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文