yhknight / odata4j

Automatically exported from code.google.com/p/odata4j
0 stars 0 forks source link

$filter expressions that traverse a collection property generate invalid JPA queries #249

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Launch the Northwind JPA example
2. In the browser, go to 
http://localhost:8886/NorthwindJpaProducerExample.svc/Customers?$filter=Orders/O
rderId eq 10643.  Orders is a collection property of the Customers entity.  
Boom.
3. To see the actual error, you'll need to look in 
org.odata4j.producer.jpa.Chain (line 34).  I wasn't able to see any logging or 
other way to get the exception.

What is the expected output? What do you see instead?
I'd expect to see a single customer - the customer that had order number 10643. 
 Instead I get the following exception:

An exception occurred while creating a query in EntityManager: 
Exception Description: Error compiling the query [SELECT t0 FROM Customers t0 
WHERE t0.Orders.OrderId = 10643], line 1, column 37: invalid navigation 
expression [t0.Orders.OrderId], cannot navigate collection valued association 
field [Orders].

This is with EclipseLink, though a similar exception results with Hibernate as 
the entity manager.  

The query should use a join instead of the multi-step WHERE clause it uses.  So 
it should actually be:  
SELECT t0 FROM Customers t0 JOIN t0.Orders t1 WHERE t1.OrderId = 10643

What version of the product are you using? On what operating system?
Tried in 0-7-0 and 0-8-0-SNAPSHOT.  OS is OSX 10.8.

Please provide any additional information below.
I describe the issue in the Google group:  
https://groups.google.com/forum/?fromgroups=#!topic/odata4j-discuss/rB95a0zY9f8.
  The code in GenerateJPQLCommand and JPQLGenerator doesn't do any expression 
parsing with the goal of building joins except for processing of a possible 
navigation property.  As a result all that happens is that JPQLGenerator 
replacesthe expression's /'s with .'s and calls it a day.  I believe this used 
to work in versions of Hibernate prior to 3.2.3, but no longer.  

I have created a fix for this, where I've updated both GenerateJPQLCommand and 
JPQLGenerator to make JPQLGenerator more stateful - I've shifted the 
responsibility for maintaining the state of the evolving query to that class 
and it is also responsible for assembling the final query.  I will provide the 
patch once I've done a little more testing but it seems to work.  I don't think 
it has any bad side effects.  I'll leave it up to the consensus to decide if my 
fix is actually a good one though :)

Original issue reported on code.google.com by m...@syple.com.au on 20 Mar 2013 at 5:29