Hi.
Let us suppose we have a class Building which have a set of another classes Floors. We want to get the Building and its first floor. So our code may be like this:
Code:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Tuple> cr = builder.createTupleQuery();
Root<Building> root = cr.from(Building.class);
Join<?, ?> join = root.join("floors");
cr.where(builder.equal(join.get("number"), 1));
cr.multiselect(root, join);
This code works perfect, because we get SQL query like this:
Code:
[java] Hibernate:
[java] select
[java] building0_.beanId as beanId0_0_,
[java] floors1_.beanId as beanId0_1_,
[java] building0_.description as descript2_0_0_,
[java] building0_.UUID as UUID0_0_,
[java] building0_.name as name0_0_,
[java] building0_.address as address2_0_,
[java] floors1_.description as descript2_0_1_,
[java] floors1_.UUID as UUID0_1_,
[java] floors1_.name as name0_1_,
[java] floors1_.building_beanId as building3_1_1_,
[java] floors1_.number as number1_1_
[java] from
[java] Building building0_
[java] inner join
[java] Floor floors1_
[java] on building0_.beanId=floors1_.building_beanId
[java] where
[java] floors1_.number=1
But if our code like this:
Code:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Tuple> cr = builder.createTupleQuery();
Root<Building> root = cr.from(Building.class);
Join<?, ?> join = root.join("floors");
cr.where(builder.equal(root.join("floors").get("number"), 1));
cr.multiselect(root, join);
We get wrong result of query (Building with ALL floors), because SQL code is:
Code:
[java] Hibernate:
[java] select
[java] building0_.beanId as beanId0_0_,
[java] floors1_.beanId as beanId0_1_,
[java] building0_.description as descript2_0_0_,
[java] building0_.UUID as UUID0_0_,
[java] building0_.name as name0_0_,
[java] building0_.address as address2_0_,
[java] floors1_.description as descript2_0_1_,
[java] floors1_.UUID as UUID0_1_,
[java] floors1_.name as name0_1_,
[java] floors1_.building_beanId as building3_1_1_,
[java] floors1_.number as number1_1_
[java] from
[java] Building building0_
[java] inner join
[java] Floor floors1_
[java] on building0_.beanId=floors1_.building_beanId
[java] inner join
[java] Floor floors2_
[java] on building0_.beanId=floors2_.building_beanId
[java] where
[java] floors2_.number=1
Is behaivour of second code expected?
P.S.: If this is bug I can provide TestCase for Hibernate, if any.