如何解决org.hibernate.hql.ast.QuerySyntaxException:异常

时间:2014-08-26 06:44:55

标签: java mysql xml hibernate hql

我想用hibernate查询从数据库中检索单个值,

这是我的java片段

    String hql="select groupid from group_details where groupname= :groupname and clientid= :clientid";
    Query query=session.createQuery(hql);
    query.setParameter(groupname, groupname);
    query.setParameter(clientid, clientid);
    int result = (int) query.uniqueResult();
    System.out.println(result);

我有一个bean类GroupDetails,其中包含settergetter字段方法。

我的映射文件是,

groupDetails.hbm.xml

<hibernate-mapping>
<class name="com.aurodisplay.its.beans.GroupDetails"    table="group_details">
    <id column="groupid" name="groupid" type="java.lang.Integer"></id>
    <property column="clientid" name="clientid" type="java.lang.Integer"></property>
    <property column="groupname" name="groupname" type="java.lang.String"></property>

</class>
</hibernate-mapping>

在Hibernate conf文件中,我映射了上面的文件,

的hibernate.cfg.xml

<mapping resource="com/aurodisplay/its/xmlFiles/groupDetails.hbm.xml"/>

我收到了以下错误,

org.hibernate.hql.ast.QuerySyntaxException: group_details is not mapped [select groupid from group_details where groupname= :groupname and clientid= :clientid]
org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:257)
org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:1)

您的查询是HQL查询而不是SQL查询,要使用您的类名,请使用GroupDetails代替您的BDD表名group_details,如下所示:

String hql="select groupid from GroupDetails where groupname= :groupname and clientid= :clientid";

编辑:

我认为您的setParameter也存在问题,第一个参数是您的字段类名称而不是值本身,因此请使用:

query.setParameter("groupname", groupname);
query.setParameter("clientid", clientid);

而不是:

query.setParameter(groupname, groupname);
query.setParameter(clientid, clientid);

希望这有帮助,