2

when joining I get one select per row. Solution is batch fetch but I dont want that annotation everywhere...

http://eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_batchfetch.htm

Why do I even need this? One select per row is awful... How can I set this globally? Cheers

Karl Kildén
  • 2,415
  • 22
  • 34

1 Answers1

0

Maybe not the ideal solution, but you may try to use JPA hints along with Java generics:

public <T> TypedQuery<T> 
createBatchQuery(String ql, Class<T> clazz, String type, String size, String relation) {
    return em.createQuery(jpql, clazz)
             .setHint(QueryHints.BATCH_TYPE, type)
             .setHint(QueryHints.BATCH_SIZE, size)
             .setHint(QueryHints.BATCH, relation);
}

The above query may then be used globally and extended with concrete query implementations according to you needs, i.e.

String jpql = "SELECT c FROM Country c WHERE c.name = :name"; // or @NamedQuery
TypedQuery<Country> q = createBatchQuery(jpql, Country.class, "JOIN", "64", "c.cities");
q.setParameter("name", "Australia");
Country c = q.getSingleResult();

Articles on this topic:

wypieprz
  • 7,981
  • 4
  • 43
  • 46
  • The syntax is not really helping because c.cities is not as typesafe and the general API would be more verbose. Also if you use a higher level api then em for queries (deltaspike data Is what I use) it's not possible to set like that – Karl Kildén Jan 24 '15 at 11:58
  • `c.cities` follows JPQL convention, thus needs to be specified as string in both `TypedQuery.setHint` method and `@QueryHint` annotation. Unfortunately I don't know DeltaSpike but you can surely use `javax.persistence.EntityManager` for such queries. – wypieprz Jan 26 '15 at 19:54