Hibernate简单条件查询解决问题

发布于 2024-09-19 13:23:52 字数 1511 浏览 5 评论 0原文

我遇到了一个非常简单的条件查询问题:

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 技术交流群。

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

发布评论

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

评论(1

浮华 2024-09-26 13:23:52

我用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文