0

Given the following JPQL query:

Select p, child, subChild
from Entity parent p
left join Child parent.children child
left join SubChid child.subChildren subChild on subChild.field = 'value'

I expect to have the parent entity with only child which have subChild with field = 'value' and I also expect to have the child.subChildren filtered to only return the one matching the 'on' clause. I am wondering if this is allowed by JPA 2.1. This seems to be but I tried with eclipselink 2.5.3 and it doesn't work as expected.

The SQL query generated is correct. It returns only the expected rows but the parent entity is not filtered and is returned with the complete object graph which I don't understand.

Can someone give me some inside on how to solve that issue?

Abbadon
  • 2,513
  • 3
  • 25
  • 33
  • 1
    Your expectations do not match what JPA is required to return. The query will return the p, child and subChild that only match the results, but child.subChildren will be the full list. Your query filtering affects the results returned, not what is put into the returned entities. These are managed entities, and so are required to reflect what is in the database so JPA can know what you are changing. You should look at the returned subChild results instead of the child.subChildren list. – Chris Oct 06 '17 at 17:33
  • @Chris thx for your answer. I am also wondering how jpa provider decide if they must do additional queries to retrieve entities. Because if I run my query with join fetch and an empty persistece context the entities collection will be filtered. So it means that state is different depending on persistence context current state, this makes it quite hard to use correctly. – Abbadon Oct 07 '17 at 11:52
  • I don't know what you mean- the references should not be filtered. The only time it might not be a reflection of what is in the database is if you have made uncommitted changes in the local context. – Chris Oct 09 '17 at 01:13
  • The point is I don't want it to be what is in database! I want to have it filtered according to my where/join on clause. – Abbadon Oct 09 '17 at 08:10
  • Then do not map it. Instead, query for it and cache it in the object yourself. Otherwise, JPA requires tracking changes and it is usually better to keep it matching what is in the database to prevent odd things from happening. Providers have different ways of doing what you want though, see http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_additionalcriteria.htm – Chris Oct 10 '17 at 13:22

0 Answers0