-->
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.  [ 2 posts ] 
Author Message
 Post subject: Criteria: a way to compare foreign keys without table joins?
PostPosted: Fri Oct 06, 2017 2:27 pm 
Newbie

Joined: Wed Feb 22, 2017 4:35 pm
Posts: 19
For the criteria API, I have a case where two tables each have a foreign key that is pointing to a parent table's id. I'd imagine that, for the sake of data integrity, it makes sense to also inner join the parent table when joining those two child tables on the parent's ID, but in my case it isn't really necessary--I'd imagine the query could be faster if the parent table wasn't involved at all. When I do this sorta thing with parent A and children B and C..

cb.equal(b.get(b_.a), c.get(c_.a))

.. an inner join is still done on the parent table, even though I don't need any other fields from that table. Is there a way to do this comparison without joining the parent table? Or does hibernate still join the parent table because the end user or their schema can't really be trusted?


Top
 Profile  
 
 Post subject: Re: Criteria: a way to compare foreign keys without table joins?
PostPosted: Fri Oct 06, 2017 3:48 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The implicit joining is always done via INNER JOIN.

If you want a LEFT JOIN, you need to:

1. Express the JOIN explicitly in your Criteria/JPQL query.

Code:
Root<B> b = cq.from(B.class);
Join<B,A> a = b.join("a", JoinType.LEFT);


2. Make sure to always check with: WHERE c IS NULL or c.id = :c.id as otherwise the SQL statement will be equivalent to an INNER JOIN anyway,

Also, if B and C don't have any join between them, they will end up in a Cartesian Product, so make sure your query is fetching data efficiently too.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.