persistence@glassfish.java.net

Re: Simple foreign keys

From: <bht_at_actrix.gen.nz>
Date: Wed, 09 Dec 2009 12:51:14 +1300

Hi Chris,

Once again thanks very much for your help.

In Step 2)
in "Unnecessary fetching causes bad performance"
https://bugs.eclipse.org/bugs/show_bug.cgi?id=297127


I am not calling child.getParent(); on each and every
child.

I am calling
parentIdNew = child.getParentId();

on each and every child.

I am calling child.getParent();
only on change of parentId, i.e. when the result of
child.getParentId() changes.

To clarify this, In the testcase, I have commented out the lines that
would do what you say as follows:
// Avoid fetching the parent on every iteration.
// For this reason, use parentIdNew not parentNew.getId()
//parentNew = child.getParent();
//parentIdNew = parentNew.getId();

I hope we are on the same page.

So I can't remove the lines as you suggest because they are removed
already.

What you are saying regarding the motivation to remove the code is
clear to me and we agree on that.

Following your logic and it appears to be the same logic as mine, the
queries for the parent should now not happen but they do happen, and
that is exactly the subject of the bug.

You made a good point for changing the JPQL query to "order by
child.parentId" and I did change that now, but that does not change
the bahavior of the testcase.

I am very grateful for our discussion, and the result gives me an
indication for how to move forward in the short term in order to get
acceptable performance.

I am thinking to use raw JDBC for this (not even a native query) with
a java.sql.SQLConnection obtained from EntityManager. In a single
container managed transaction, I would like to invalidate the cache
selectively for all members of that entity class only, as I know
exactly what I am doing, and as this table only has a self-join. The
cost of it would be neglegible as this would be done only oce for the
whole batch not for every child.

Learning from this bug how easy it seems to be for EclipseLink to
(over)do it, how can I do that within the transaction?

Many thanks.

Best regards

Bernard