1答, 1)Query接口有list等方法返回一个List接口的实例,直接调用list的size方法就是结果 2)见下 2答,同1楼 public int getCount(String hqlAppend) throws HibernateCheckException { validatePojoName(); try { Query query = null; if (hqlAppend != null && hqlAppend.length() > 0) { query = session.createSQLQuery("select count(*) from "+pojoName+WHERE+hqlAppend); } else { query = session.createSQLQuery("select count(*) from "+pojoName); } Object result = query.uniqueResult(); return Integer.valueOf(result.toString()); } catch (HibernateException e) {
Print.er(e); throw new HibernateCheckException("向数据库获取模型数量未执行成功:"+pojoName); } }
谢谢楼上各位的回答,可是我的意思是。。 我访问一个页面。。执行了 100 个 UPDATE 语句。 我用什么方法能得到100这个数字?还有 我在hql里写了 where id = ? AND name = ? 但hibernate不支持 id = ? 后面的语句,直接把and 给忽略了。。 只能用SQLQuery么?
SQLQuery可以用,但不支持数据到持久化对象的映射。 楼主可以把每一次update后的受影响行数加起来保存在session中。 hql支持where id = and name = 之列的语句。前提是id和name是配置文件中已经被映射的该类的字段。如果持久类的xxxId变量映射的是数据库中的xxx_id字段,那么在hql里操作的当然是xxxId
希望这个对你有用:String q = "from User u where u.name=:myName and u.password=:myPassword"; //if wanna to search more than two, then just add "and u.[table column name]=:[anyname]"List l =(List)this.getHibernateTemplate().execute( new HibernateCallback(){ public Object doInHibernate(Session s) throws HibernateException, SQLException { Query query = s.createQuery(q); //length is the size of the columns you wanna to check. for(int i=1;i<length;i++){ //for me, i use a string to store all the column name and value which is split to a String[]; //this is to add the value to that particular column name; query.setString(pN[i], (String)pV[i]); } System.out.print(query.getQueryString()); return query.list(); } } ); //if you want to know the size of the that list, use list.size
1)Query接口有list等方法返回一个List接口的实例,直接调用list的size方法就是结果
2)见下
2答,同1楼
public int getCount(String hqlAppend) throws HibernateCheckException {
validatePojoName();
try {
Query query = null;
if (hqlAppend != null && hqlAppend.length() > 0) {
query = session.createSQLQuery("select count(*) from "+pojoName+WHERE+hqlAppend);
} else {
query = session.createSQLQuery("select count(*) from "+pojoName);
}
Object result = query.uniqueResult();
return Integer.valueOf(result.toString());
} catch (HibernateException e) {
Print.er(e);
throw new HibernateCheckException("向数据库获取模型数量未执行成功:"+pojoName);
}
}
我访问一个页面。。执行了 100 个 UPDATE 语句。
我用什么方法能得到100这个数字?还有 我在hql里写了 where id = ? AND name = ?
但hibernate不支持 id = ? 后面的语句,直接把and 给忽略了。。
只能用SQLQuery么?
楼主可以把每一次update后的受影响行数加起来保存在session中。
hql支持where id = and name = 之列的语句。前提是id和name是配置文件中已经被映射的该类的字段。如果持久类的xxxId变量映射的是数据库中的xxx_id字段,那么在hql里操作的当然是xxxId
应该多去看一下api,有难一点的问题再拿出来问啊!
2.和平常使用一样
//if wanna to search more than two, then just add "and u.[table column name]=:[anyname]"List l =(List)this.getHibernateTemplate().execute(
new HibernateCallback(){
public Object doInHibernate(Session s) throws HibernateException, SQLException {
Query query = s.createQuery(q);
//length is the size of the columns you wanna to check.
for(int i=1;i<length;i++){
//for me, i use a string to store all the column name and value which is split to a String[];
//this is to add the value to that particular column name;
query.setString(pN[i], (String)pV[i]);
}
System.out.print(query.getQueryString());
return query.list();
}
}
);
//if you want to know the size of the that list, use list.size