NHibernate.QueryException:重复的关联路径
下面的代码片段描述了我想使用查询执行的操作,但它因上述错误而崩溃。
建筑物和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该使用联接来执行此操作,正如您可能从异常中猜到的那样。您应该使用两个子查询来测试值“Blue”和“Large”是否在集合中。我不太擅长 ICriteria,但生成的 SQL 应该如下所示,并且我认为这在 ICriteria 中是可能的(使用
Subqueries.PropertyIn
)。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
).