zoukankan      html  css  js  c++  java
  • hibernate获取count(*)

    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    type Exception report
     
    message
     
    description The server encountered an internal error () that prevented it from fulfilling this request.
     
    exception
     
    org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
        org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
     
    root cause
     
    java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
        net.java2000.notepad.service.impl.hibernate.PostServiceHibernateImpl.countAllSubject(PostServiceHibernateImpl.java:24)
        org.apache.jsp.jsph.index_jsp._jspService(index_jsp.java:93)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
     
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.

    1.主要代码

    Query query = getSession().createQuery(hql);
    query.setInteger(0, id);
    Long count=(Long) query.iterate().next();//返回值是Long
    return count;

    2.解决Long转为Integer的问题

    代码:

    Query query = getSession().createQuery(hql);
    query.setInteger(0, id);
    Number count= (Number) query.iterate().next();//返回值是Long
    return count.intValue();

    测试:

    Integer id=1;
    String hql="SELECT COUNT(s.suiteExamination.suiteId) FROM LogSuiteExamination s WHERE s.suiteExamination.suiteId=?";
    Integer count=(Integer) new LogSuiteExaminationDaoImpl().queryLogCountById(hql, id);
    System.out.println(count);

    因为无论是Long还是Integer,还是BigInteger,它们都是Number的子类,所以用Number去cast这个结果是肯定不会错的,然后,最好定义在Number里面这些方法可以统一的返回需要的类型;

     byte byteValue() 
              以 byte 形式返回指定的数值。 
    abstract  double doubleValue() 
              以 double 形式返回指定的数值。 
    abstract  float floatValue() 
              以 float 形式返回指定的数值。 
    abstract  int intValue() 
              以 int 形式返回指定的数值。 
    abstract  long longValue() 
              以 long 形式返回指定的数值。 
     short shortValue() 
              以 short 形式返回指定的数值。 


    =====================================================
    java.lang 
    类 Number
    java.lang.Object
      java.lang.Number
    所有已实现的接口: 
    Serializable 
    直接已知子类: 
    AtomicInteger, AtomicLong, BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short 

  • 相关阅读:
    轻量级监控平台之java进程监控脚本
    轻量级监控平台之cpu监控
    通过jgit一次性升级fastjson版本
    Jedis Unexpected end of stream & java.net.SocketException: Broken pipe问题解决思路
    程序日志停止滚动问题排查
    前后端hosts配置访问问题解决思路
    mysql慢日志分析组件安装
    redis集群搭建
    redis哨兵环境搭建
    pyspider基础
  • 原文地址:https://www.cnblogs.com/shumengru/p/5335992.html
Copyright © 2011-2022 走看看