Raghu
Raghu

Reputation: 1324

How to solve org.hibernate.hql.ast.QuerySyntaxException: Exception

I want to retrieve a single value from database with hibernate query,

Here is my java snippet

    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);

I have a bean class GroupDetails which has setter and getter methods for fields.

my mapping file is,

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>

in Hibernate conf file , I mapped above file,

hibernate.cfg.xml

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

I am getting following error ,

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)

Can anyone help me in this please.

Upvotes: 1

Views: 1545

Answers (1)

albciff
albciff

Reputation: 18507

Your query is a HQL query instead of SQL query, to work with you have to use your class name, so use GroupDetails instead of your BDD table name group_details as follows:

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

EDIT:

I think there is also a problem with your setParameter, the first parameter is your field class name not the value itself, so use:

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

Instead of:

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

Hope this helps,

Upvotes: 1

Related Questions