views:

67

answers:

1

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?

A: 

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

michel