Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Native queries & batch hint

Hi EclipseLink users and developers.

We are using native queries, and we really like the "eclipselink.batch" feature. However, these two don't work together. When we try to set this hint for a native query to batch retrieve a dependant collection, we get a NPE. The reason is that the descriptor field in the query is not initialized, and a lookup for a string token is attempted on a null reference. The NPE can be avoided if the following line is added in EntityManagerImpl.createNativeQueryInternal(String, Class):
query.changeDescriptor(getServerSession());
After this operation, the batch kind of works. Kind of means that it doesn't throw a NPE, and the second SQL is actually issued to the database. The bad thing is that the sql that is issued is incorrect. So, for example, this is the native query:
SELECT p.* FROM Parent p WHERE p.id < 10
Parent contains a collection of Child (unidirectional, a link table is used), and I set the batch hint (of course the patched EntityManagerImpl is needed). The second SQL is:
SELECT c.*, p.id FROM parent p, child c, link_table l WHERE l.parent_id = p.id and l.child_id = c.id
whereas it should also contain the additional where condition like this;
and p.id < ?
with the parameter bound to 10, reflecting the where clause from the first query. This is how it works for JPQL queries. Without it, the second query will load all Child elements that have a Parent as these are all inner joins. With thousands of Parents with many Child elements, this gets bad.

I would like to ask if there is any reason why hints are not supported for native queries, and if it is just an overlooking, is it possible to make the batch work like for JPQL? What would have to be done in order for this to work?

The tests are based on EL 2.0.0.

Attached is a zip package with a maven project with a test case and a patched EntityManagerImpl to prevent the NPE mentioned.

Best regards,
RafaƂ

Attachment: test.zip
Description: test.zip


Back to the top