persistence@glassfish.java.net

RE: JPA query: null values in the query result

From: Gordon Yorke <gordon.yorke_at_oracle.com>
Date: Fri, 11 May 2007 14:13:15 -0400

Try this query:
SELECT assocEnt, e.description FROM Entity e LEFT OUTER JOIN e.associatedEntity assocEnt WHERE e.description LIKE 'foo'

Because there may be no associated entity you must perform an OUTER JOIN.
--Gordon

-----Original Message-----
From: ales [mailto:ales_at_mincol.com]
Sent: Friday, May 11, 2007 7:35 AM
To: persistence_at_glassfish.dev.java.net
Subject: JPA query: null values in the query result


Hi all,
please help me if possible. I have the following situation:

- Entity
- AssociatedEntity
- Entity has field associatedEntity
- database table contains many Entity records WITHOUT references to
AssociatedEntities
- when I use the following JPA query: SELECT e.associatedEntity.name,
e.description FROM Entity e WHERE e.description LIKE 'foo' nothing is
returned (there are Entity descriptions with value foo)
- when I use the following JPA query: SELECT e.associatedEntity,
e.description FROM Entity e WHERE e.description LIKE 'foo' nothing is
returned (there are Entity descriptions with value foo)
- when I change Entity records to have references to AssociatedEntities
mentioned queries return data

I do not understand why mentioned queries when Entity records do not
have references to AssociatedEntities do not return any data. "JSR 220:
Enterprise JavaBeansTM,Version 3.0" in "4.8.3 Null Values in the Query
Result" says:

If the result of a query corresponds to a association-field or
state-field whose value is null, that null
value is returned in the result of the query method.

IMHO expression "e.associatedEntity" ends with association-field so null
value should be returned in the result. It seems for me that behaviour
of the Hibernate framework is the same.

Best regards,

Ales


Implementation-Title: TopLink Essentials
Implementation-Version: 2.0-b43-beta3 (04/18/2007)