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