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中的?的索引可以自己隨意任命了..


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM