hibernate 查询语句的一个小问题
在查询语句如下:
hqlstr1= "select u from User u where u.name=? and u.age=? ";
hqlstr2= "select u from User u where u.name=:name and u.age=:age ";
当在执行查询时,要对 点位符参数 和 命名参数 进行赋值时分别要用什么方法?
用 setString(0, " ") 形式还是 setParameter(0, " ")这种形式???
序列号是从0开始吧?
thanks in advance
------解决方案--------------------都是setString(0, " ")这样的形式把,是从0开始的
------解决方案--------------------Query Q = session.createQuery(from User u where u.name=:name and u.age=:age);
query.setString (name, tom);
query.setInteger(age, 21);
list result = query.list();
------解决方案--------------------Query Q = session.createQuery( "from User u where u.name=? and u.age=? ");
query.setString (0, "tom ");
query.setInteger(1, new Integer(21));
list result = query.list();
------解决方案-------------------- 基本类型最好用相应的如setString、setLong等
setParameter可以像下面这样用
hql= "select u from User u where u.department=:department ";
...
query.setParameter( "department ", new Department(1));
class Department{
private int id;
//getter and setter
public Department(int id){
this.id=id;
}
}
}