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

混江龙づ霸主 提交于 2020-01-07 02:56:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!