Hibernate:关联的 HQL 连接查询未按预期结果
Hibernate: HQL join query on association doesnt result as expected
我有两个类
User
和 article
User
与文章
有 one-to-many
关系
I want to get an article by the user's username
and article's id
select distinct user.articles
from User as user inner join user.articles as article
where article.user.username = 'sammer' and article.id = 8
如果 ID 与用户撰写的文章不匹配,我会得到与预期相同的结果,只有 where article.user.username = 'sammer'
或没有结果
而我将另一个限制添加为 article.id =8
。它再次显示所有结果。它应该提供唯一的结果或根本没有结果。
Ex: Suppose user has written article with id's 8,9,10. when I hit the above query, it gives all the 3 results. But if I change the id to anything except the 8,9,10. It doesn't show any result. but it shows all the results when the id is any of the 8,9 or 10
请多多指教。我可能做错了什么。
感谢您的宝贵时间!
Select条满足筛选条件的文章:
select a
from Article a
where a.user.username = 'sammer' and a.id = 8
I want to get an article by the user's username and article's id
就像@DraganBozanovic 说的那样,您只需要查询 Article
而不是像 select distinct user.articles ...
那样浏览 User
的文章集
Suppose user has written article with id's 8,9,10. when I hit the
above query, it gives all the 3 results. But if I change the id to
anything except the 8,9,10. It doesn't show any result. but it shows
all the results when the id is any of the 8, 9 or 10
当条件 article.user.username = 'sammer' and article.id = 8
匹配时,您已找到链接到某个用户的特定 Article
,但您随后 询问该用户的文章 select distinct user.articles
而不是获取您刚刚找到的文章。这就是为什么你最终得到了所有用户的文章 8, 9, 10.
如果您想获得第 8 篇文章,请执行以下操作:
select distinct article
from User as user inner join user.articles as article
where article.user.username = 'sammer' and article.id = 8
当 article.id 8 不存在或其链接的用户有其他 [=27] 时,return 文章或 none/exception =]姓名.
我有两个类
User
和 article
User
与文章
one-to-many
关系
I want to get an article by the user's
username
and article'sid
select distinct user.articles
from User as user inner join user.articles as article
where article.user.username = 'sammer' and article.id = 8
如果 ID 与用户撰写的文章不匹配,我会得到与预期相同的结果,只有 where article.user.username = 'sammer'
或没有结果
而我将另一个限制添加为 article.id =8
。它再次显示所有结果。它应该提供唯一的结果或根本没有结果。
Ex: Suppose user has written article with id's 8,9,10. when I hit the above query, it gives all the 3 results. But if I change the id to anything except the 8,9,10. It doesn't show any result. but it shows all the results when the id is any of the 8,9 or 10
请多多指教。我可能做错了什么。 感谢您的宝贵时间!
Select条满足筛选条件的文章:
select a
from Article a
where a.user.username = 'sammer' and a.id = 8
I want to get an article by the user's username and article's id
就像@DraganBozanovic 说的那样,您只需要查询 Article
而不是像 select distinct user.articles ...
User
的文章集
Suppose user has written article with id's 8,9,10. when I hit the above query, it gives all the 3 results. But if I change the id to anything except the 8,9,10. It doesn't show any result. but it shows all the results when the id is any of the 8, 9 or 10
当条件 article.user.username = 'sammer' and article.id = 8
匹配时,您已找到链接到某个用户的特定 Article
,但您随后 询问该用户的文章 select distinct user.articles
而不是获取您刚刚找到的文章。这就是为什么你最终得到了所有用户的文章 8, 9, 10.
如果您想获得第 8 篇文章,请执行以下操作:
select distinct article
from User as user inner join user.articles as article
where article.user.username = 'sammer' and article.id = 8
当 article.id 8 不存在或其链接的用户有其他 [=27] 时,return 文章或 none/exception =]姓名.