Hibernate简单条件查询解决问题
我遇到了一个非常简单的条件查询问题:
sess .createCriteria(user.class, "user")
.user_c.add(Restrictions.eq("user.status", 1))
.user_c.createAlias("user.userCategories","ucs")
.add(Restrictions.eq("ucs.category_id",1));
.add(Restrictions.eq("ucs.category_id",'yes'));
.add(Restrictions.eq("ucs.category_id",2));
.add(Restrictions.eq("ucs.category_id",'no')).list();
当然这会导致没有用户。
在SQL中我也尝试过:
Select * FROM users user , ec_user_category uc where
uc.user_login = user.login AND
uc.status = 1 AND
((uc.category_id = 1 AND uc.value = 'yes') AND (uc.category_id = 2 AND uc.value = 'no'))
当然也没有用户
在SQL中有两种解决方案,不是很有效我认为:
Select a.login from
(Select user.login , COUNT(*) as counter FROM users user , ec_user_category uc
where user.status = 1
and uc.user_login = user.login
and
((uc.category_id = 1 and uc.value = 'yes')
OR (uc.category_id = 2 and uc.value = 'no'))
GROUP BY user.login) a where a.counter = 2
其他:
Select * FROM users u
JOIN user_category uc on uc.user_login = u.login
JOIN user_category uc2 on uc2.user_login = u.login
WHERE (uc.category_id = 1 and uc.value = 'yes')
AND (uc2.category_id = 2 and uc2.value = 'no')
这些结果给了我很好的信息,但我不认为这是正确的方法。以及我将如何使用 Criteria API 在 Hibernate 中实现此功能。
或者我的数据库中需要一个新表吗?
I'm stuck with a very simple criteria query problem:
sess .createCriteria(user.class, "user")
.user_c.add(Restrictions.eq("user.status", 1))
.user_c.createAlias("user.userCategories","ucs")
.add(Restrictions.eq("ucs.category_id",1));
.add(Restrictions.eq("ucs.category_id",'yes'));
.add(Restrictions.eq("ucs.category_id",2));
.add(Restrictions.eq("ucs.category_id",'no')).list();
of course this results no user.
in SQL I also tried it:
Select * FROM users user , ec_user_category uc where
uc.user_login = user.login AND
uc.status = 1 AND
((uc.category_id = 1 AND uc.value = 'yes') AND (uc.category_id = 2 AND uc.value = 'no'))
also no user of course
two solutions in SQL, not very effective I think:
Select a.login from
(Select user.login , COUNT(*) as counter FROM users user , ec_user_category uc
where user.status = 1
and uc.user_login = user.login
and
((uc.category_id = 1 and uc.value = 'yes')
OR (uc.category_id = 2 and uc.value = 'no'))
GROUP BY user.login) a where a.counter = 2
other:
Select * FROM users u
JOIN user_category uc on uc.user_login = u.login
JOIN user_category uc2 on uc2.user_login = u.login
WHERE (uc.category_id = 1 and uc.value = 'yes')
AND (uc2.category_id = 2 and uc2.value = 'no')
these results are give me the good information, but I don't think this is the correct way. And how I'm gonna implements this in Hibernate with the Criteria API.
Or need I a new Table in my DB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用 HQL 修复了这个问题,因为使用 Criteria 不可能两次加入同一个关联
http://opensource.atlassian.com/projects/hibernate/browse/HHH-879
I Fix this with HQL, because with Criteria it isn't possible to join the same association twice
http://opensource.atlassian.com/projects/hibernate/browse/HHH-879