users@glassfish.java.net

A query with LEFT JOIN FETCH returns duplicates when it shouldn't

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Fri, 2 Mar 2007 15:43:12 +0100

Hello there,
I have strange feeling there is something wrong with the way LEFT JOIN
FETCH works with TopLink Essentials (I am using Glassfish v1 upadte 1
patch 1).

This is how does the query look like:

SElECT s FROM BankStatement s
LEFT JOIN FETCH s.statementPositions
WHERE s.id = ?1

So, it is expected that there will be only one result or not at all,
as you cannot have more than one BankStatement entities with the same
ID.

I have a bank statement with 2 statement positions. When I invoke
#getSingleResult(), NonUniqueResultException is thrown. When I invoke
#getResultList(), I am getting two identical BankStatement entities,
each is owning collection with same statement positions.

For me it looks like a bug, 'WHERE s.id=?1' should always guarantee
there is at most one result, so there should be no need to add
DISTINCT (which is sometimes very problematic as many databases cannot
accept DISTINCT when large binary/character strings are in fields. I
know, that in SQL such a query could return many rows, but this is not
SQL.

Is that correct?

Regards,
Witold Szczerba