java - 标准总和投影

标签 java spring hibernate hibernate-criteria

我尝试对一列求和,但出现空指针异常,我不知道自己做错了什么。

ChartVentaVendedores chart = new ChartVentaVendedores();
Criteria facturas = getSession().createCriteria(Factura.class);
facturas.add(Restrictions.like("estado", "CONFIRMADO"));
facturas.add(Restrictions.between("fechaCreacion", fromDate, toDate));

Criteria vendedorFactura = facturas.createCriteria("idVendedor");
vendedorFactura.add(Restrictions.eq("idUsuario", idVendedor));
facturas.setProjection(Projections.sum("total"));

Long counter = (Long) facturas.uniqueResult();
int value = counter.intValue();
chart.setValue(counter.intValue());            
chart.setDate(fromDate);

即使我没有进行连接,我仍然得到 null

我试图将 Long counter = (Long) facturas.uniqueResult(); 的类型更改为 BigInteger 但我仍然遇到相同的错误。

我的类(class):

@Entity
@Table(name = "factura")
public class Factura implements Serializable {

    private int idFactura;
    private Cliente cliente;
    private String estado;
    private Usuarios idVendedor;
    private Date fechaCreacion;
    private Date fechaModificacion;
    private Integer usuarioCreacion;
    private Integer usuarioModificacion;
    private BigInteger total;
    private String tipoFactura;

    ....
}

最佳答案

我认为,您的 Projections.sum("total") 不能对 BigInteger 求和,因为它没有自动装箱。

如果可以,请尝试将 BigInteger 更改为 IntegerLong

关于java - 标准总和投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35972808/

相关文章:

java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException : Communications link failure

java - 如何在 Spring Boot 中将属性文件值读入字符串集

java - hibernate 中的 ID 类型

java - Hibernate EntityListeners - PrePersist 方法在持久调用之前修改模型 - 如果持久调用失败怎么办?

java - 取消缩进或线性化 XML

java - 如何更改 JTable 单元格中的数据?

java - 有没有办法根据 Spring 配置文件禁用 Testcontainer?

spring - FlywayAutoConfiguration 没有 "see"数据源

spring - 无法使用 spring 获取 eclipselink 的 EntityManager 的事件 session

hibernate - 条件API动态排序