-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Duplicate joins when using fetch join
PostPosted: Wed Nov 30, 2016 1:03 pm 
Beginner
Beginner

Joined: Wed Sep 03, 2003 9:31 am
Posts: 26
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?


Top
 Profile  
 
 Post subject: Re: Duplicate joins when using fetch join
PostPosted: Wed Nov 30, 2016 3:17 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Remove these lines:

Code:
Join<Entity1, Entity2> fromEntity2 = fromEntity1.join("entity2");
Join<Entity2, Entity3> fromEntity3 = fromEntity2.join("entity3");


and replace these lines:

Code:
Join<Entity1, Entity2> fromEntity2 = (Join<Entity1, Entity2>) fromEntity1.fetch("entity2");
Join<Entity2, Entity3> fromEntity3 = (Join<Entity2, Entity3>) fromEntity2.fetch("entity3");


Top
 Profile  
 
 Post subject: Re: Duplicate joins when using fetch join
PostPosted: Tue Dec 06, 2016 9:43 am 
Beginner
Beginner

Joined: Wed Sep 03, 2003 9:31 am
Posts: 26
Thanks for the tip.

I did it and got this compilation error:
Code:
Cannot cast from Fetch<Object,Object> to Join<Entity1,Entity2>


However when I did like this:
Code:
Join<Entity1, Entity2> fromEntity2 = (Join)fromEntity1.fetch("entity2");
Join<Entity2, Entity3> fromEntity3 = (Join)fromEntity2.fetch("entity3");


it worked, but I got a type safety compilation warning.

Anyway, the "double joins" are gone.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.