eclipselink icon indicating copy to clipboard operation
eclipselink copied to clipboard

EntityManager#find don't support multiple query hints

Open MelleD opened this issue 3 years ago • 5 comments

Currently there is no way to execute the EntityManager e.g. entityManager#find with multiple query hints with the same keys because a Java map is expected.

One suggestion was to pass QueryHints comma separately. Is that possible? Are there any better suggestions? Here is the issue directly in the specification. A discussion which is the best way would be great. https://github.com/eclipse-ee4j/jpa-api/issues/306

MelleD avatar Apr 11 '21 18:04 MelleD

+1 but subject of this issue should be EntityManager#find don't support multiple query hints of same type

patric-r avatar Jul 27 '22 14:07 patric-r

Any news?

MelleD avatar Sep 13 '22 07:09 MelleD

For the following:

@NamedQuery(
        name="AllEmployees",
        query="SELECT e FROM Employee e",
        hints={
                @QueryHint(name=QueryHints.QUERY_RESULTS_CACHE_SIZE, value="200"),
                @QueryHint(name=QueryHints.QUERY_RESULTS_CACHE_SIZE, value="500")
        }
)

should the result cache size be 700, 500, 200 or an error?

lukasj avatar Sep 13 '22 08:09 lukasj

An error.

But this issue is more about how to specify multiple hints of the same type when using EntityManager.find(), e.g. for betch fetching?

patric-r avatar Sep 13 '22 11:09 patric-r

Some require simple boolean value/string value, some a fixed-value string and combining multiple options at once may make no sense, others a number etc. So what particular option do you mean? Or do you think that something like @QueryHint(name=QueryHints.QUERY_RESULTS_CACHE_SIZE, value="200,500") to use the same sample makes sense?

In general, if one needs to pass multiple query hints with the same keys to the query, then one should get a query and call setHint on it multiple times, ie following way:

            TypedQuery<Project> query = em.createQuery("SELECT p FROM Project p", Project.class);
            query.setHint(QueryHints.BATCH, "p.teamMembers");
            query.setHint(QueryHints.BATCH, "p.teamMembers.projects");
            query.setHint(QueryHints.BATCH, "p.teamMembers.phoneNumbers.owner");
            query.setHint(QueryHints.BATCH_TYPE, BatchFetchType.IN);
            query.setHint(QueryHints.FETCH, "p.teamMembers.address");
            query.setHint(QueryHints.FETCH, "p.teamMembers.phoneNumbers");

            List<Project> results = query.getResultList();

instead of using em.find

lukasj avatar Sep 13 '22 12:09 lukasj