简体   繁体   English

java.lang.ClassCastException:java.math.BigInteger无法强制转换为java.lang.Long

[英]java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

I want to return number of rows using native sql. 我想使用本机sql返回行数。 But console says me java.math.BigInteger cannot be cast to java.lang.Long . 但是控制台说我java.math.BigInteger cannot be cast to java.lang.Long What's wrong? 怎么了? This is my method: 这是我的方法:

public Long getNumRows(Integer id){

        Session session = null;

        session = this.sessionFactory.getCurrentSession();
        Query query = session
                .createSQLQuery("SELECT COUNT(*) FROM controllnews WHERE news_id="
                        + id + ";");
        List firstResult = query.list();

        return (Long) firstResult.get(0);


    }

Use BigInteger#longValue() method, instead of casting it to Long : 使用BigInteger#longValue()方法,而不是将其强制转换为Long

return firstResult.get(0).longValue();

Seems like firstResult.get(0) returns an Object . 看起来像firstResult.get(0)返回一个Object One option is to typecast to BigInteger : 一种选择是对BigInteger进行类型转换:

return ((BigInteger)firstResult.get(0)).longValue();

But don't do this. 但是不要这样做。 Instead use the way provided by Nambari in comments. 而是使用Nambari在评论中提供的方式。 I'm no user of Hibernate, so I can't provide Hibernate specific solution. 我不是Hibernate的用户,所以我无法提供Hibernate特定的解决方案。

I faced same problem, I used Hibernate Scalar queries as suggested in the comment of @Rohit Jain's answer . 我遇到了同样的问题,我使用了@Rohit Jain的回答评论中建议的Hibernate Scalar查询。 Thanks @nambari for comment. 感谢@nambari的评论。

Coming to the problem we have, 来到我们遇到的问题,

Query query = session
            .createSQLQuery("SELECT COUNT(*) FROM controllnews WHERE news_id="
                    + id + ";");  

These will return a List of Object arrays (Object[]) with scalar values for each column in the controllnews table. 这些将返回一个Object of Array数组(Object []),其中包含controllnews表中每列的标量值。 Hibernate will use ResultSetMetadata to deduce the actual order and types of the returned scalar values. Hibernate将使用ResultSetMetadata来推断返回的标量值的实际顺序和类型。

To avoid the overhead of using ResultSetMetadata, or simply to be more explicit in what is returned, one can use addScalar(): 为了避免使用ResultSetMetadata的开销,或者只是在返回的内容中更明确,可以使用addScalar():

Query query = session
            .createSQLQuery("SELECT COUNT(*) as count FROM controllnews WHERE news_id="
                    + id + ";").addScalar("count", LongType.INSTANCE);  

This will return Object arrays, but now it will not use ResultSetMetadata but will instead explicitly get the count column as Long from the underlying resultset. 这将返回Object数组,但现在它不会使用ResultSetMetadata,而是显式地从底层结果集中将count列作为Long

How the java.sql.Types returned from ResultSetMetaData is mapped to Hibernate types is controlled by the Dialect. 如何将从ResultSetMetaData返回的java.sql.Types映射到Hibernate类型由Dialect控制。 If a specific type is not mapped, or does not result in the expected type, it is possible to customize it via calls to registerHibernateType in the Dialect. 如果未映射特定类型,或者未产生预期类型,则可以通过调用Dialect中的registerHibernateType来自定义它。


You can use Query#setParameter method to avoid any mistakes in query as 您可以使用Query#setParameter方法来避免查询中的任何错误

Query query = session
           .createSQLQuery("SELECT COUNT(*) as count FROM controllnews WHERE news_id= :id")
           .addScalar("count", LongType.INSTANCE).setParameter("id",id);  

One confusion when you refer Scalar queries docs 4.0, addScalar method has second parameter Hibernate.LONG , remember it has been deprecated since Hibernate version 3.6.X 当您引用标量查询 docs 4.0时,一个混乱, addScalar方法有第二个参数Hibernate.LONG ,记得它自Hibernate版本3.6.X以来已被弃用
Here is the deprecated document , so you have to use LongType.INSTANCE 这是不推荐使用的文档 ,因此您必须使用LongType.INSTANCE

Related link 相关链接

  String query ="select count(*) from tablename";       
  Number count = (Number) sessionFactory.getCurrentSession().createSQLQuery(query).uniqueResult();
  Long value = count.longValue();

I tried this it works well 我试过这个效果很好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.math.BigInteger 无法转换为 java.lang.Long - java.math.BigInteger cannot be cast to java.lang.Long java.lang.Long无法强制转换为java.math.BigInteger - java.lang.Long cannot be cast to java.math.BigInteger ClassCastException: java.math.BigInteger 在连接到 MySQL 时不能转换为 java.lang.Long - ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long on connect to MySQL Sqoop2无法从mysql导入到hdfs,原因是:“引起原因:java.lang.ClassCastException:java.math.BigInteger无法转换为java.lang.Long” - Sqoop2 failed import from mysql to hdfs with “Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long” 尝试连接后,我一直收到 java.math.bigInteger 无法转换为 java.lang.Long 的问题 - I keept getting a java.math.bigInteger cannot be cast to java.lang.Long iafter trying to connect java.lang.ClassCastException:无法将java.math.BigInteger强制转换为model.APRecord - java.lang.ClassCastException: java.math.BigInteger cannot be cast to model.APRecord java.lang.ClassCastException:不能强制转换为java.lang.Long - java.lang.ClassCastException: cannot be cast to java.lang.Long java.lang.ClassCastException:java.math.BigDecimal 不能转换为 java.lang.Long - java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Long java.lang.ClassCastException:无法将java.lang.Integer强制转换为java.math.BigInteger HTTP状态500 - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.math.BigInteger HTTP Status 500 java.math.BigInteger与java.lang.Long不兼容 - java.math.BigInteger incompatible with java.lang.Long
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM