[code="java"]
public void updDb(Session session){
try{
String hql = "select * from Sample";
Query qr = session.createQuery(hql);
List list = qr.list();
for(int i = 0; i < list.size(); i++){
Sample s = (Sample)list.get(i);
System.out.println(s.getSampleId()+" : "+s.getName());
}
}
catch(HibernateException e){
e.printStackTrace();
}
[/code]
下面是错误:
2010-11-25 13:35:48,567 ERROR [org.hibernate.hql.PARSER] - line 1:8: unexpected token: *
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
怎么解决?
自己看出问题了,是查询语句写错了,现在想问个问题是使用hibernate查询,前面不能写'select [字段]...'么
这个很好解决啊··你要搞明白HQL语句和SQL语句的区别,HQL语句是面向对象的,不需要知道底层数据库表中的各个字段名,因为Hibernate会根据映射文件去匹配,而SQL语句必须知道各个字段名。
Hibernate的一大特性就是会自动生成SQL语句:
以你上面的例子来看,你是想查询与Sample类关联的表的所有记录,这时候的HQL语句
只要写成
String hql="form Sample";即可,
当你要进行多表链接查询的时候再加select。
hql 一般写成"from Entry ...."前面不加select
HQL语句很SQL是不一样的 好好研究下你的语句 HQL是以对象来查询的