0

With Hibernate as provider.

In terms of performance (or others), which type of parameter is better to use? and why?

Positional

TypedQuery<Client> query = em.createQuery
  ("FROM Client c WHERE c.clientId = ?1",Client.class);
query.setParameter(1, clientId);

or Named

TypedQuery<Client> query = em.createQuery
  ("FROM Client c WHERE c.clientId = :clientId",Client.class);
query.setParameter("clientId", clientId);
user454322
  • 7,300
  • 5
  • 41
  • 52
  • I have never listened to any study about this kind of comparison, but I would like to see the data sources and process about it if they exists. –  Apr 25 '12 at 04:24
  • "Performance" is *not* a useful consideration here. So, use the form that is more clear. This can change from query to query. Positional in an `INSERT ... VALUES` might make most sense, for instance. –  Apr 25 '12 at 04:31

1 Answers1

5

You should not be really considering performance in this case, named parameters helps in improving the readability of the code. Even if it is slower by few nano second or so you should be sticking to it.

TypedQuery<Client> query = em.createQuery
  ("FROM Client c WHERE c.clientId = :clientId",Client.class);
query.setParameter("clientId", clientId);

In the above lines it is very clear that you are setting the value for clientId. It is simple, crisp and clear and that how you want to code.

ManuPK
  • 11,623
  • 10
  • 57
  • 76
  • 2
    Moreover, using named parameters makes HQL/JPQL query refactoring a lot easier and less error-prone than with positional parameters – kyiu Apr 25 '12 at 06:26