项目里面有三个实体类 如:
package com.newer.pojo;public class Country {
private Integer CountryID;
private String CountryName;
private Medal medal;
      ......
package com.newer.pojo;public class Event {
private Integer EventID;
private String EventName;
private Medal medal;
   ......package com.newer.pojo;import java.util.Date;public class Medal {
private Integer MedalID;
private String MedalType;//奖牌类型
private Date AwardDate;
private Country country;
private Event event;
现在是要求在网页上显示 :名次,得奖的国家,金牌数,银牌数,铜牌数 
 名次是按金牌数排名
如果先不考虑排名 Query 语句这样写执行时报错(Query q = session.createQuery("select c.CountryName,count(m.MedalType='金牌'),count(m.MedalType='银牌'),count(m.MedalType='铜牌') from Medal as m right outer join m.country as c ");下面是网页报的错:
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.hibernate.hql.ast.QuerySyntaxException: expecting CLOSE, found '=' near line 1, column 39 [select c.CountryName,count(m.MedalType='金牌'),count(m.MedalType='银牌'),count(m.MedalType='铜牌') from com.newer.pojo.Medal as m right outer join m.country as c ]
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)
com.newer.dao.MedalDao.findAll(MedalDao.java:23)
com.newer.web.MedalOrderAction.execute(MedalOrderAction.java:10)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         ......如果要排名的话该怎么写Hql 语句?我再次感谢大家了啊 

解决方案 »

  1.   

    这个需要行列转换了.使用原生SQL来查询吧.select xx.Country_Name,isnul(a.num,0),isnull(b.sum,0),isnull(c.sum,0) from country as xx left join (select Country_id,count(id) as num from Medal where MedalType='金牌' group by Country_id) as a on xx.id=a.Country_id left join
    (select Country_id,count(id) as num from Medal where MedalType='铜牌' group by Country_id) as b on xx.id=b.Country_id left join 
    (select Country_id,count(id) as num from Medal where MedalType='铜牌' group by Country_id) as c on xx.id=c.Country_id
    order by a.num
      

  2.   

    select xx.Country_Name,isnul(a.num,0),isnull(b.sum,0),isnull(c.sum,0) from country as xx left join (select Country_id,count(id) as num from Medal where MedalType='金牌' group by Country_id) as a on xx.id=a.Country_id left join
    (select Country_id,count(id) as num from Medal where MedalType='银牌' group by Country_id) as b on xx.id=b.Country_id left join  
    (select Country_id,count(id) as num from Medal where MedalType='铜牌' group by Country_id) as c on xx.id=c.Country_id
    order by a.num
      

  3.   

    HQL对复杂的查询支持不好executeSqlquery 执行sql语句
      

  4.   

    HQL语句怎么写 QBC语句也可以啊 使用原生SQL来查询感觉太不简短了
      

  5.   

    写个原生SQL。Query q = session.createSQLQuery("原生SQL语句").如果是多表查询。结果集是在页面的。。哪就得在把得query结果集在封装下。在页面取这个封装的值就可以啦!