[英]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 这是我的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);
I have a bean class GroupDetails
which has setter
and getter
methods for fields. 我有一个bean类
GroupDetails
,它具有用于字段的setter
和getter
方法。
my mapping file is, 我的映射文件是
groupDetails.hbm.xml 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 conf文件中,我映射了上面的文件,
hibernate.cfg.xml 的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. 谁能帮我这个忙。
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: 您的查询是HQL查询而不是SQL查询,要与您一起使用,必须使用您的类名,因此请使用
GroupDetails
而不是BDD表名group_details
,如下所示:
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: 我认为
setParameter
也存在问题,第一个参数是您的字段类名称而不是值本身,因此请使用:
query.setParameter("groupname", groupname);
query.setParameter("clientid", clientid);
Instead of: 代替:
query.setParameter(groupname, groupname);
query.setParameter(clientid, clientid);
Hope this helps, 希望这可以帮助,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.