我试图通过实际上使用另一个对象的休眠查询来获取 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,因为在生成最终查询之前,我在代码中检查了很多条件。谢谢。