Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 363798

Summary: Invalid order by entity.field query is not detected and undefined result is returned
Product: z_Archived Reporter: Sei Syvalta <syvalta>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: REOPENED --- QA Contact:
Severity: normal    
Priority: P4 CC: tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard:
Attachments:
Description Flags
Test case. Just run mvn clean install and observe the test failure. none

Description Sei Syvalta CLA 2011-11-15 06:12:18 EST
Build Identifier: 2.3.0.v20110604-r9504

Given following entities:
@Entity
public class TestEntity {
    @ManyToOne
    private RelatedEntity relation;
}

@Entity
public class RelatedEntity {
    private String name;
}

Query q = em.createQuery("SELECT te FROM TestEntity te ORDER BY te.relation.name");
q.getResultList();

Result contains only those TestEntities having non-null relation field. Looking at the generated SQL the reason seems to be that EclipseLink does select from multiple tables instead of left join (i.e. SELECT FROM Table1, Table2 instead of SELECT FROM Table1 LEFT JOIN Table2).

Somewhat related but different issue: http://bugs.eclipse.org/bugs/show_bug.cgi?id=294092.


Reproducible: Always

Steps to Reproduce:
1. Run the attached testcase
2.
3.
Comment 1 Sei Syvalta CLA 2011-11-15 06:42:36 EST
Created attachment 207018 [details]
Test case. Just run mvn clean install and observe the test failure.
Comment 2 Tom Ware CLA 2011-11-15 08:32:28 EST
What happens if you run this query?

SELECT te FROM TestEntity te left join te.relation r ORDER BY
r.name
Comment 3 Sei Syvalta CLA 2011-11-15 09:51:29 EST
Thanks, that works fine. However, in our case the queries are programmatically generated so unfortunately it's not very straightforward workaround.
Comment 4 Tom Ware CLA 2011-11-16 08:21:27 EST
The only reference to this in the JPA specification that I can find is from section 4.4.4 - Patch Expressions:

"Path expression navigability is composed using “inner join” semantics. That is, if the value of a non-terminal
field in the path expression is null, the path is considered to have no value, and does not participate
in the determination of the result."

I couild not find anything that indicated this should behave any differently in an ORDER BY clasue.

Based on the above, I am closing this bug.  I can't see a away to change this behavior without specifically disobeying the spec.
Comment 5 Sei Syvalta CLA 2011-11-16 10:13:59 EST
In my interpretation 4.4 is talking about WHERE clause.

In 4.9: "The ORDER BY clause allows the objects or values that are returned by the query to be ordered."

It doesn't hint at all that ORDER BY would be allowed to affect the result of the query.
Comment 6 Tom Ware CLA 2011-11-16 13:01:57 EST
The part of the spec that I think relates is this:

"the path is considered to have no value"

To me, something with no value cannot participate in the ORDER BY
Comment 7 Sei Syvalta CLA 2011-11-18 03:12:42 EST
Tom, you are correct. The spec lead kindly confirmed that it is an invalid query (disallowed by the first order_by requirement in 4.9).

I'll reopen and update the subject. Instead of executing the query and returning undefined result EclipseLink should not allow an the query to be executed.
Comment 8 Tom Ware CLA 2011-11-18 07:51:46 EST
Reclosing based on feedback from JPA spec comittee confirming what I have said above.
Comment 9 Tom Ware CLA 2011-11-18 07:53:43 EST
Sorry, didn't read what you had done.

Reopening.
Comment 10 Eclipse Webmaster CLA 2022-06-09 10:27:13 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink