Hibernate占位符?和:及JPA

王朝学院·作者佚名  2016-05-28
窄屏简体版  字體: |||超大  

Hibernate占位符?和:及JPA小结一下hibernate占位符.

1.最常见的?占位符.

String hql = "select a from Apple a where a.color=? a.weight>?";Query query = session.createQuery(hql);query.setParameter(0, "red");query.setParameter(1, "10");

下标从0开始,最常见的.这个让人头疼的是数?个数...

2.以一个变量名的形式占位.

String hql = "select a from Apple a where a.color=:pcolor a.weight>:pweight";Query query = session.createQuery(hql);query.setParameter("pcolor", "red");query.setParameter("pweight", "10");

这个就不存在数?个数的问题了.应该是比较方便的一种方法了

3.JPA方式,这种方式是1的改良版本..

String hql = "select a from Apple a where a.color=?2 a.weight>?5";Query query = session.createQuery(hql);query.setParameter("2", "red");query.setParameter("5", "10");

方法1中的?的索引可以自己随意任命了..

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航