为什么我的 HQL 忽略Where 子句
我有这样的代码:
IList<Foo1> ou = session.CreateQuery("from Foo1 as f1 JOIN FETCH f1.foo2 as f2 USING (id_foo1) where f2.foo3.year ='2004'").List<Foo1>();
它返回 Foo1 中的所有对象并忽略 where 子句。为什么?大概哪里出错了?
接下来的实验:
(1) from Foo2 as f2 where f2.year = '2004' //is OK
(2) from Foo1 as f1 JOIN FETCH f1.foo2 as f2 USING (id_foo1) //is OK
(3) from Foo1 as f1 JOIN FETCH f1.foo2 as f2 USING (id_foo1) where f2.year = '2004' //returns the same result as (2)
I have this code:
IList<Foo1> ou = session.CreateQuery("from Foo1 as f1 JOIN FETCH f1.foo2 as f2 USING (id_foo1) where f2.foo3.year ='2004'").List<Foo1>();
It returns all objects from Foo1 and ignores where clause. Why? Where is probably mistake?
Next experiments:
(1) from Foo2 as f2 where f2.year = '2004' //is OK
(2) from Foo1 as f1 JOIN FETCH f1.foo2 as f2 USING (id_foo1) //is OK
(3) from Foo1 as f1 JOIN FETCH f1.foo2 as f2 USING (id_foo1) where f2.year = '2004' //returns the same result as (2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,这对我来说也有点困惑,但在一个例子中我发现了这样的注释:“你不需要 WHERE 子句,因为 Hibernate“知道”如何根据映射匹配主键和外键”。这个“Where”与 JOIN 条件相关,因此只需尝试删除您的 USING() 部分。
I had the same problem and it is little bit confusing for me as well, but in one example I found this note: "You don't need the WHERE clause as Hibernate "knows" how to match primary and foreign keys based on the mapping." This "Where" was related to JOIN condition so just try to remove your USING() part.