user454322
user454322

Reputation: 7649

In JPA which type of parameter is better to use "positional/named"?

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);

Upvotes: 0

Views: 2236

Answers (1)

ManuPK
ManuPK

Reputation: 11829

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.

Upvotes: 5

Related Questions