0

我试图通过实际上使用另一个对象的休眠查询来获取 count(*) 和 sum() 的对象。这就是我正在做的事情:

String query = select new org.rangde.domain.AggregatedCount(count(*), sum(lps.loanAmount - lps.loanPledged - lps.loanRaised)) from LoanProfileSnapshot lps where  lps.loanState in (:loanStates) 
List<AggregatedCount> counts = getHibernateTemplate().findByNamedParam(query, params, values);
return counts.size() > 0 ? counts.get(0) : null;

这是 AggregatedCount 类(已删除 getter 和 setter)

public class AggregatedCount {
    private int id;
    private BigInteger count;
    private BigDecimal sum;

    public AggregatedCount(){}

    public AggregatedCount(BigInteger count, BigDecimal sum){
        this.count = count;
        this.setSum(sum);
    }
}

这是我运行查询时遇到的异常。

Unable to locate appropriate constructor on class [org.domain.AggregatedCount] ...  nested exception is org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [org.domain.AggregatedCount] ... 

我尝试将总和设为 Double 和 BigInteger,但仍然遇到同样的异常。

任何帮助,将不胜感激。我更愿意为此坚持使用 findByNamedParam,因为在生成最终查询之前,我在代码中检查了很多条件。谢谢。

4

1 回答 1

0

虽然我的回答似乎有点晚了,但也许它会对其他人有所帮助.. 必要的数据类型是 Long ,因为计数和总和取决于实体中的数据类型。

它还取决于您使用的休眠版本: https ://community.jboss.org/wiki/HibernateCoreMigrationGuide35?_sscc=t

public class AggregatedCount {

private Long count;
private Long sum;


   public AggregatedCount(Long count, Long sum){
      this.count = count;
      this.setSum(sum);
   }

}

于 2013-09-04T09:57:51.060 回答