-->
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.  [ 1 post ] 
Author Message
 Post subject: Why Left Outer Join results in Cartesian Product ?!
PostPosted: Tue Jun 09, 2009 6:43 am 
Newbie

Joined: Mon Jun 08, 2009 6:28 am
Posts: 1
Hello,

Let's say we have:

- Table TableA (idA is a primary key) which will be considered main.
- Table TableB (idB, idA is a composite primary key)
- Records in table B may, or may not exist.
- A.b maps as one-to-many to Table b

We want to get access to:

Specific record set from TableA with joined specific elements from TableB. By specific i mean narrowed down by TableB.pk.idB

Example:

TableA
(idA, x) VALUES (1, 'aaa');
(idA, x) VALUES (2, 'aaa');
(idA, x) VALUES (3, 'aaa');
(idA, x) VALUES (4, 'bbb');
(idA, x) VALUES (5, 'ccc');
(idA, x) VALUES (6, 'ddd');

TableB
(idA, idB, z) VALUES (1, 1, '11');
(idA, idB, z) VALUES (1, 2, '12');
(idA, idB, z) VALUES (3, 1, '31');
(idA, idB, z) VALUES (4, 1, '41');
(idA, idB, z) VALUES (5, 1, '51');
(idA, idB, z) VALUES (5, 2, '52');

SELECT a FROM
TableA a
LEFT OUTER JOIN FETCH TableA.b b
WHERE
a.x = :x AND
b.pk.idB = :idb

x = '11'
idb = 1;

Which results in SQL:
SELECT
...
FROM
TableA,
TableB
WHERE
TableA.x = 'aaa' AND
TableA.idA = TableB.idA AND
TableB.idB = 1;

QUESTIONS:
- Why there is no left join in resulting SQL ???
- SQL uses artesian product (or inner join) which filtered according to where clause returns only two rows! Which is incorrect! Result should be three records:

TableA (1, 'aaa') with TableA.b as 1 element list of TableB (1, 1, '11')
TableA (2, 'aaa') with TableA.b as 0 element list or null
TableA (3, 'aaa') with TableA.b as 1 element list of TableB (3, 1, '31')

All would be well IF hibernate would decide to use LEFT JOIN.

Can anyone shed some light on this issue ?
How can it be solved ?
Maybe i am doing something incorrect ?


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

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.