问题
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);
回答1:
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.
来源:https://stackoverflow.com/questions/10309314/in-jpa-which-type-of-parameter-is-better-to-use-positional-named