Hello Bernard,
While lazy is used, you are calling child.getParent(); on each and every
child making the lazy relationship behave as if it were eager. Remove
this line and you will not see the query for the parent for each child -
actually you call it twice and don't seem to use it other than for is Id
anyway. Remove both calls and the query for the parent will not
happen. Also, you can use "order by child.parentId" in your JPQL query
to avoid the table join in the resulting SQL
I'm guessing since someone needs to look at the bug still, that
EclipseLink is hitting the database for find methods when an update gets
issued since it can't be sure that the selection criteria used wasn't
modified by the updated. This way it allows the database to decide if
the work done through native sql or JPQL would affect the results,
instead of trying to use what could be wrong in the cache. Refreshing
what is in the cache isn't an option, since it cannot refresh the
managed entities without potentially wiping out user changes
prematurely. This doesn't seem neccessary for a finder or 1:1 type
query, so someone will need to look into the bug further.
Best Regards,
Chris
bht_at_actrix.gen.nz wrote:
> Hi Chris,
>
> Many thanks for reading the testcase and your detailed explanation.
>
> I am convinced that the cache can no longer be trusted after an update
> query has run, and I can understand that the update query did not
> refresh the entities in the cache. I also understand that what I am
> seeing in the log file is not necessarily happening in the sequential
> order that I would expect.
>
> Therefore I am happy with the fact that EntityManager fetches those
> entities once again after an update query, even after it had fetched
> them before for a result list.
>
> I am making multiple fruitless efforts to not touch the related parent
> entity more than once with FetchType.LAZY and by not reading them as
> an entity but reading only its foreign key value via the different
> mapping that I created for that purpose.
>
> What really stands out in that testcase and what I fail to understand
> is that Entitymanager absolutely consistently fetches the same parent
> entity, after the update queries have already run, multiple times,
> e.g. 35 times in a row, exactly as many times as I read its children
> even while I am not accessing the parent at all. This is in step 2)
> where no update queries are run, and where entities are updated only
> via their setter methods. I really don't know what would explain this
> behavior and that leads me to think it is a bug.
>
> Regards
>
> Bernard
>
>
>