我想用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
,它对字段有setter
和getter
方法。我的映射文件是,
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)
有人能帮我一下吗。
最佳答案
查询是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);
希望能帮上忙,