1. 程式人生 > >org.hibernate.hql.internal.ast.QuerySyntaxException: expecting OPEN, found 'from' near line 1, colum

org.hibernate.hql.internal.ast.QuerySyntaxException: expecting OPEN, found 'from' near line 1, colum

程式碼如下:

package cn.com.query;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import cn.com.dao.User;
import cn.com.util.Hibernate_Utils;

public class Demo1 {
	/*
	 * author:命運的信徒 date:2018/12/05 arm:完成CRM的客戶的條件查詢
	 */
	@Test
	public void demo() {
//統計索引查詢
		Session session=Hibernate_Utils.openSession();
		Transaction tx=session.beginTransaction();
		Query q=session.createQuery("select count from User");
		//對於查詢結果的只有一種,可以採取以下方法
		Long i=(Long) q.uniqueResult();
		System.out.println(i);
		tx.commit();
		session.close();
	}

}

原因:

HQL語法錯誤;count是關鍵字,

應該是

        Query q=session.createQuery("select count(*) from User");