[hibernate-issues] [Hibernate-JIRA] Created: (HHH-5736) Problem with "not" function of CriteriaBuilder

Vyacheslav Dimitrov (JIRA) noreply at atlassian.com
Tue Nov 16 12:08:13 EST 2010


Problem with "not" function of CriteriaBuilder
----------------------------------------------

                 Key: HHH-5736
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5736
             Project: Hibernate Core
          Issue Type: Bug
    Affects Versions: 3.6.0
            Reporter: Vyacheslav Dimitrov


Let us suppose that we have entity "Floor" with property of "number". 
Then we want to create query with the help of Criteria API: we want to get all floors and exclude floors with number of 2 and 3. Let us suppose that our code is:

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> cr = builder.createTupleQuery();

    Root<Floor> root = cr.from(Floor.class);
    cr.multiselect(root);
    Predicate p1 = builder.equal(root.get("number"), new Integer(2));
    Predicate p2 = builder.equal(root.get("number"), new Integer(3));
    cr.where(builder.not(builder.or(p1, p2)));

So we get query:
     [java] Hibernate: 
     [java]     select
     [java]         floor0_.beanId as beanId0_,
     [java]         floor0_.building_beanId as building3_1_,
     [java]         floor0_.number as number1_ 
     [java]     from
     [java]         Floor floor0_ 
     [java]     where
     [java]         floor0_.number=2 
     [java]         or floor0_.number=3

This query is wrong. Function "not" doesn't work.

But for query "get all floors and exlclude floor with number 2" this code works:
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> cr = builder.createTupleQuery();

    Root<ru.petrsu.nest.son.Floor> root = cr.from(ru.petrsu.nest.son.Floor.class);
    cr.multiselect(root);
    Predicate p = builder.equal(root.get("number"), new Integer(2));
    cr.where(builder.not(p));

We get correct query:
     [java] Hibernate: 
     [java]     select
     [java]         floor0_.beanId as beanId0_,
     [java]         floor0_.building_beanId as building3_1_,
     [java]         floor0_.number as number1_ 
     [java]     from
     [java]         Floor floor0_ 
     [java]     where
     [java]         floor0_.number<>3


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list