I'm using Hibernate 5.2.5 and migrating my criteria queries to JPA criteria queries as suggested in the documentation.
Everything is working fine until now but I noticed something.
I have a CriteriaQuery like this:
Code:
CriteriaQuery<Entity1> query = builder.createQuery(Entity1.class);
Root<Entity1> fromEntity1 = query.from(Entity1.class);
Join<Entity1, Entity2> fromEntity2 = fromEntity1.join("entity2");
Join<Entity2, Entity3> fromEntity3 = fromEntity2.join("entity3");
fromEntity1.fetch("entity2").fetch("entity3"); // I assume I have to do that to fetch the entire objects, right?
List<Entity1> entities = hs.createQuery(query
.select(fromEntity1)
.where(builder.and(
builder.equal(fromEntity1.get("idStatus"), 1),
builder.like(fromEntity3.get("name"), "a%"))))
.getResultList();
It works fine, but the in the generated SQL I have the same joins twice.
Code:
select
entity1_.codcoligada as codcolig1_94_0_,
entity2_.codcoligada as codcolig1_48_1_,
entity3_.codigo as codigo1_47_2_,
from table1 entity1_
inner join table2 aluno1_ on entity1_.codcoligada=aluno1_.codcoligada and entity1_.ra=aluno1_.ra
inner join table3 pessoa2_ on aluno1_.codpessoa=pessoa2_.codigo
inner join entity2 table2_ on entity1_.codcoligada=entity2_.codcoligada and entity1_.ra=entity2_.ra
inner join table3 entity3_ on entity2_.codpessoa=entity3_.codigo
where entity1_.codstatus=1
and (entity3_.nome like ?)
As you can see the generated sql has the entity2' and entity3' joins made twice. Is this correct or am I doing something wrong?