如何在NHibernate HQL中使用SUM

本文关键字:SUM HQL NHibernate | 更新日期: 2023-09-27 18:11:45

我正在尝试使用NHibernate的HQL的SUM,但当执行查询抛出异常A first chance exception of type 'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll和结果不返回。我怎么能这么做呢?

尝试。

public IList<Conta> findAllContasReceber() {
            ISession _session = getSession();
            IList<Conta> list = _session.CreateQuery("SELECT SUM(c.valorFinal) " + 
                                                     "FROM Conta c " + 
                                                     "WHERE (c.tipoConta = 1) AND (c.status = 0) " + 
                                                     "GROUP BY c.dtVencimento, c.cliente " + 
                                                     "ÖRDER BY c.dtVencimento ASC "
                                                     )                
                .List<Conta>();
            return list;
        }

实体

[Serializable]
    public class Conta {
        public virtual long id                      { set; get; }        
        public virtual Cliente cliente              { set; get; }
        public virtual String historico             { set; get; }
        public virtual DateTime dtLancamento        { set; get; }
        public virtual DateTime dtVencimento        { set; get; }
        public virtual decimal valorPagar           { set; get; } //total vendas
        public virtual decimal valorAcrescimo       { set; get; } //total acrescimo
        public virtual decimal valorFinal           { set; get; } //total pagar
        public virtual DateTime dtPagamento         { set; get; }
        public virtual int tipoConta                { set; get; }  //1 receber, 2 pagar
        public virtual PlanoDeConta planoConta      { set; get; }
        public virtual int status                   { set; get; } //0 ativa, 1 fechada, 2 cancelada, 3 aguardando pagamento
        public virtual Venda venda                  { set; get; }

        public Conta() {
        }
    }

如何在NHibernate HQL中使用SUM

解决。在寻找了很多建议之后,我找到了一种方法来做我想做的事。

这就是答案。

public IList<Conta> findAllContasReceber() {
            ISession _session = getSession();
            String SQL = "SELECT new Conta(c.cliente, c.historico, c.dtLancamento, c.dtVencimento, SUM(c.valorPagar), SUM(c.valorAcrescimo), SUM(c.valorFinal), c.status) " + 
                         "FROM Conta c WHERE (c.tipoConta = 1) AND (c.status = 0) " + 
                         "GROUP BY c.cliente, c.dtVencimento " + 
                         "ORDER BY c.dtVencimento ";
            IList<Conta> list = _session.CreateQuery(SQL).List<Conta>();
            return list;
        }

然后在Conta类中创建了一个构造函数来接收参数

[Serializable]
    public class Conta {
        public virtual long id                      { set; get; }        
        public virtual Cliente cliente              { set; get; }
        public virtual String historico             { set; get; }
        public virtual DateTime dtLancamento        { set; get; }
        public virtual DateTime dtVencimento        { set; get; }
        public virtual decimal valorPagar           { set; get; } //total vendas
        public virtual decimal valorAcrescimo       { set; get; } //total acrescimo
        public virtual decimal valorFinal           { set; get; } //total pagar
        public virtual DateTime dtPagamento         { set; get; }
        public virtual int tipoConta                { set; get; }  //1 receber, 2 pagar
        public virtual PlanoDeConta planoConta      { set; get; }
        public virtual int status                   { set; get; } //0 ativa, 1 fechada, 2 cancelada, 3 aguardando pagamento
        public virtual Venda venda                  { set; get; }

        public Conta() {
        }
        public Conta(Cliente cliente, String historico, DateTime dtLancamento, DateTime dtVencimento, 
                    decimal valorPagar, decimal valorAcrescimo, decimal valorFinal, int status){
                        this.cliente = cliente;
                        this.historico = historico;
                        this.dtLancamento = dtLancamento;
                        this.dtVencimento = dtVencimento;
                        this.valorPagar = valorPagar;
                        this.valorAcrescimo = valorAcrescimo;
                        this.valorFinal = valorFinal;
                        this.status = status;
        }
    }

它最终100%工作