这两天在做一个项目,遇见了一个hibernate的奇葩问题:
表结构:
?
我想对立面的一些数据进行排序,聚集多数:
模板:
StringBuilder sb = new StringBuilder("select new mybus.model.MRouterBaseCount( COUNT(");
sb.append(keyWord);
sb.append(") , ");
sb.append(keyWord);
sb.append(" ) from QuestionBase group by ");
sb.append(keyWord);
keyWord是根据要求定得,要求就是图片立面红色得那个。
结果对了一个,其余三个全部错。我将打印了的SQL语句放到数据库里面去,是可以得到正确结果的:;
select new mybus.model.MRouterBaseCount( COUNT( bqDestinationArea ) , bqDestinationArea ) from QuestionBase group by bqDestinationArea
Hibernate:
select
count(questionba0_.bq_destination_area) as col_0_0_,
questionba0_.bq_destination_area as col_1_0_
from
mybus.question_base questionba0_
group by
questionba0_.bq_destination_area
{"resultValue":[{"keyCount":5,"keyName":"科创园区"},{"keyCount":3,"keyName":"游仙区"},{"keyCount":1,"keyName":"涪城区"},{"keyCount":1,"keyName":"警钟街"}],"totalValue":4}
//这是正确的结果(看图片可以知道)
select new mybus.model.MRouterBaseCount( COUNT( bqStartCommunity ) , bqStartCommunity ) from QuestionBase group by bqStartCommunity
Hibernate:
select
count(questionba0_.bq_start_community) as col_0_0_,
questionba0_.bq_start_community as col_1_0_
from
mybus.question_base questionba0_
group by
questionba0_.bq_start_community
{"resultValue":[{"keyCount":3,"keyName":"1s"}],"totalValue":1}
//这是错误的结果(看图片也可以知道)
显然,这是一个很奇葩的问题!求解释
------解决方案--------------------
没遇到过,把count(keyWord)那块改成count(*)试试看呢?count(字段名)没什么意义的,不像sum等函数
------解决方案--------------------
+1
------解决方案--------------------
你把SQL放到数据库中查 看看返回几条结果 如果只返回一行 说明你查询有问题
------解决方案--------------------
查出来的list.size()是几?
------解决方案--------------------
调整一下hibernate的日志配置吧,看看更详细的信息