在JPA中哪种类型的参数更适合使用“位置/命名”? [英] In JPA which type of parameter is better to use "positional/named"?

查看:104
本文介绍了在JPA中哪种类型的参数更适合使用“位置/命名”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



关于性能(或其他),哪种类型的参数更适合使用?
为什么?



位置

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

命名

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


解决方案

在这种情况下,您不应该真正考虑性能, 命名参数有助于提高代码的可读性。即使速度比纳秒慢,你应该坚持下去。

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

在上面的几行中,非常清楚您正在设置的clientId 简单,清晰,清晰,您想要编码


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

解决方案

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.

这篇关于在JPA中哪种类型的参数更适合使用“位置/命名”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆