Setup

Imagine the following entities: 

A Company with a 1:Many to AddressLocation and another 1:Many to
AddressPostal.

AddressLocation and AddressPostal are two different kind of addresses. They
are both subclasses of Address.



The goal


The goal is to have to following two queries which both have the same
result:

Match just by the name of the company.
Match by the name of the company and street of the location address.


The result is always the name of the company and the street of the location
address. 

Version1: Hand written native queries


Query by company name only



Query by company name and street of location address



The question is if it is possible to formulate these queries with JPA Query
Language in such a way, that also just one join is used. 

So I tried the versions below.

Version 2: JPA Query

Query by company name only




Results is the following SQL:


Query by company name and street of location address




Results is the following SQL:


Well, both queries do almost what I original wanted, i.e. just two joins,
except, that they do not limit the queries to the location address.
Therefore the queries are not semantically correct. 


Version 3: JPA Query with Type construct

To limit the query to a specific type, in my case the type AddressLocation,
the type construct must be used.

Query by company name only



Results is the following SQL:


Query by company name and street of location address



Results is the following SQL:


>From a conceptional point of view these queries do exactly what I want,
except, that two joins are used. Actually I have no idea why in both cases
the second join to Address is done, the alias t2 is only used in the select
(t2.id) where I do not need it. 

Conclusion and Question

The question is, if there is another way to express the JPA Query so that
only one join is used and that only addresses of type AddressLocation are
taken into account. Or is the only solution, to step back to native queries
which would really be a pity because we would like to use higher level APIs
like the Criteria API or Querydsl which rely on JPA Queries. I.e. as long as
at the JPA Query level these kind of problems are not solved, we can not use
Criteria API or Querydsl. 

--
View this message in context: 
http://openjpa.208410.n2.nabble.com/Unnecessary-Join-when-using-the-Type-construct-in-JPA-Query-tp7424993p7424993.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to