如何在Hibernate中编写sql Native Query

时间:2013-02-12 08:52:29

标签: hibernate

Query query = session.createSQLQuery(
"SELECT ColumnName, COUNT(*) FROM xyz GROUP BY ColumnName")
.addEntity(xyz.class);
return query.list();

但它在会话中显示错误(会话无法解析) 如果有人能帮我执行这个查询,我将非常感激。

先谢谢

1 个答案:

答案 0 :(得分:0)

似乎您的Session对象未声明。

还检查导入:

import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;

典型的Hibernate会话代码:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Session session = sessionFactory.openSession();

然后使用您的代码:

Query query = session.createSQLQuery(
"SELECT ColumnName, COUNT(*) FROM xyz GROUP BY ColumnName")
.addEntity(xyz.class);
return query.list();