hibernate2 是int的,hibernate3.0开始我都用long了

解决方案 »

  1.   

    我select count(uid) 后返回的是一个list
    int count_user = (Integer)DaoFactory.getDemomiap2DaoInstance().Getsql(hqlcount).get(0);
    来得到的 没报错
      

  2.   

    hibernate不太熟悉  但是看错误是类型转换的问题
      

  3.   

    Long 是基本类型 
    Integer是 int的封装型 
    是不是在这里出现了问题?最好是都改成封装型的  Hibernate 读取数据的时候是 通过 class文件反射的 
    "但是,有几个网友却是正好相反,他们直接运行报 java.lang.ClassCastException: java.lang.Integer 改成Integer却正常了。 
    希望大家使用Hibernate比较熟悉的人,确认一下,大家随意使用 count(*) 然后读取看看,到底是Integer,还是 Long "
     
    这个应该是 配置映射的时候 你们之间有差别
      

  4.   

    跟hibernate版本有关,3之后都是long
      

  5.   

    测试代码:         public void countAllSubject() {
        this.session = this.sessionFactory.getSessionFactory().openSession();
        this.session.beginTransaction();
        System.out.println(session.createQuery("select count(*) from User").iterate().next().getClass());
       
     }
     public static void main(String args[]) {
     BaseDAO b=new BaseDAO();
     b.countAllSubject();
     }显示结果15:07:51,046  INFO Environment:479 - Hibernate 3.1.3
    15:07:51,062  INFO Environment:509 - hibernate.properties not found
    15:07:51,062  INFO Environment:525 - using CGLIB reflection optimizer
    15:07:51,078  INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
    15:07:51,140  INFO Configuration:1308 - configuring from resource: /hibernate.cfg.xml
    15:07:51,140  INFO Configuration:1285 - Configuration resource: /hibernate.cfg.xml
    15:07:51,390  INFO Configuration:469 - Reading mappings from resource: pojo/hbm/authority.hbm.xml
    15:07:51,500  INFO HbmBinder:309 - Mapping class: pojo.Authority -> authority
    15:07:51,515  INFO Configuration:469 - Reading mappings from resource: pojo/hbm/character.hbm.xml
    15:07:51,546  INFO HbmBinder:309 - Mapping class: pojo.Character -> characters
    15:07:51,546  INFO Configuration:469 - Reading mappings from resource: pojo/hbm/characterAuthority.hbm.xml
    15:07:51,578  INFO HbmBinder:309 - Mapping class: pojo.CharacterAuthority -> characterAuthority
    15:07:51,593  INFO Configuration:469 - Reading mappings from resource: pojo/hbm/user.hbm.xml
    15:07:51,609  INFO HbmBinder:309 - Mapping class: pojo.User -> user
    15:07:51,609  INFO Configuration:469 - Reading mappings from resource: pojo/hbm/userCharacter.hbm.xml
    15:07:51,625  INFO HbmBinder:309 - Mapping class: pojo.UserCharacter -> usercharacter
    15:07:51,640  INFO Configuration:1419 - Configured SessionFactory: null
    15:07:51,843  WARN RootClass:210 - composite-id class does not override equals(): pojo.UserCharacter
    15:07:51,843  WARN RootClass:215 - composite-id class does not override hashCode(): pojo.UserCharacter
    15:07:51,859  WARN RootClass:210 - composite-id class does not override equals(): pojo.CharacterAuthority
    15:07:51,859  WARN RootClass:215 - composite-id class does not override hashCode(): pojo.CharacterAuthority
    15:07:51,875  INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
    15:07:51,875  INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
    15:07:51,875  INFO DriverManagerConnectionProvider:45 - autocommit mode: false
    15:07:51,875  INFO DriverManagerConnectionProvider:80 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://127.0.0.1:3306/test
    15:07:51,875  INFO DriverManagerConnectionProvider:86 - connection properties: {user=root, password=****}
    15:07:52,156  INFO SettingsFactory:77 - RDBMS: MySQL, version: 5.0.22-community-nt
    15:07:52,156  INFO SettingsFactory:78 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.8 ( Revision: ${svn.Revision} )
    15:07:52,171  INFO Dialect:103 - Using dialect: org.hibernate.dialect.MySQLDialect
    15:07:52,171  INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
    15:07:52,171  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    15:07:52,171  INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
    15:07:52,171  INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
    15:07:52,171  INFO SettingsFactory:136 - JDBC batch size: 15
    15:07:52,171  INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
    15:07:52,171  INFO SettingsFactory:144 - Scrollable result sets: enabled
    15:07:52,171  INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): enabled
    15:07:52,171  INFO SettingsFactory:160 - Connection release mode: auto
    15:07:52,187  INFO SettingsFactory:184 - Maximum outer join fetch depth: 2
    15:07:52,187  INFO SettingsFactory:187 - Default batch fetch size: 1
    15:07:52,187  INFO SettingsFactory:191 - Generate SQL with comments: disabled
    15:07:52,187  INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
    15:07:52,187  INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    15:07:52,187  INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
    15:07:52,187  INFO SettingsFactory:203 - Query language substitutions: {}
    15:07:52,187  INFO SettingsFactory:209 - Second-level cache: enabled
    15:07:52,187  INFO SettingsFactory:213 - Query cache: disabled
    15:07:52,187  INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.EhCacheProvider
    15:07:52,187  INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
    15:07:52,187  INFO SettingsFactory:237 - Structured second-level cache entries: disabled
    15:07:52,187  INFO SettingsFactory:257 - Echoing all SQL to stdout
    15:07:52,203  INFO SettingsFactory:264 - Statistics: disabled
    15:07:52,203  INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled
    15:07:52,203  INFO SettingsFactory:283 - Default entity-mode: pojo
    15:07:52,234  INFO SessionFactoryImpl:154 - building session factory
    15:07:52,250  WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/F:/Program%20Files/MyEclipse%206.0/myeclipse/eclipse/plugins/com.genuitec.org.hibernate.eclipse_6.0.0.zmyeclipse60020070820/myeclipse-data/3.0/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
    15:07:52,500  INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
    Hibernate: select count(*) as col_0_0_ from user user0_
    class java.lang.Integer
      

  6.   

    这个应该是跟数据库有关的
    DB2是返回java.lang.Long
    MySQL返回java.math.BigInteger
    其它数据库不知道
      

  7.   

    select count(*)
    返回的是Long类型的,
    我用mysql时是这样的。
      

  8.   

    补充一下,我的数据库用的 MySQL我想count(*) 这个应该没有关系吧,呵呵!!
      

  9.   

    本帖最后由 java2000_net 于 2008-07-25 16:24:54 编辑
      

  10.   


    我的jar包还没下好呢
    呵呵
      

  11.   

    是的 我觉得Long本来就要比integer 明智
    integer的最大值才21亿多
    而long有 9223372036854775807我是在想他为什么要把Integer变为Long
    这个有原因吗?
      

  12.   


    我用Integer 接收没有报错
      

  13.   

    推荐一个方法,
      @Override
      public int countAllSubject() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Number number=((Number) session.createQuery(
            "select count(*) from Post where idParent=0").iterate().next());
        return number.intValue();
            
      }因为无论是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 
      

  14.   

    你可以    返回值.class   看看到底什么类型。
      

  15.   

    直接弄成Number,然后自己拿出来想怎么转就怎么转(int,long).
      

  16.   

    hibernate配置了什么就是什么!!
      

  17.   


    但是我用的是hibernate3.0,对于这个错误还是java.math.BigInteger cannot be cast to java.lang.Long???