org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: limit near line 1, column 80 [ SELECT name FROM cla_hospital WHERE city='北京市' and name like '%北京%' limit 8 ] at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91) at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
但上面的sql语句直接拿到数据库是对的 。。
原因
SELECT name FROM cla_hospital WHERE city='北京市' and name like '%北京%' limit 8
你这句话是标准的sql语句,但是你在这里sessionFactory.getCurrentSession().createQuery(sql);开启的是hibernate的面向对象的语句所以不可以,你改成sessionFactory.getCurrentSession().createSQLQuery(sql);就可以了