Hibernate returns BigIntegers instead of longs

后端 未结 5 1350
猫巷女王i
猫巷女王i 2021-01-07 23:21

This is my Sender entity

@Entity
public class Sender {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long senderId;
...


...

    p         


        
相关标签:
5条回答
  • 2021-01-07 23:47

    Object database mapping is wrong. There is a casting exception here saying database field is BigInteger, but object property is long.

    BigInteger is a special class to hold unlimited size integer values. Furthermore, BigInteger can not cast to long implicitly.

    To avoid this error database field which is BigInteger should be change to long compatible type. Change it to a int type where int can be casted to long implicitly. See BigInteger.

    0 讨论(0)
  • 2021-01-07 23:49

    In older versions of Hibernate you can use

      session.createSQLQuery("select column as num from table")
     .addScalar("num", Hibernate.LONG).list();
    
    0 讨论(0)
  • 2021-01-07 23:57

    Adding to #Hedley comment to fix it globally you can add a line in SQLDialect constructor. In my project it was like:

    public PostgreSQLDialect() {
            super();
            registerHibernateType(Types.BIGINT, StandardBasicTypes.LONG.getName());
        }
    
    0 讨论(0)
  • 2021-01-07 23:57

    you can check on following link check here for BigInteger identity generator class

    0 讨论(0)
  • 2021-01-08 00:00

    The default for ".list()" in hibernate appears to be BigInteger return types for Numeric. Here's one work around:

    session.createSQLQuery("select column as num from table")
      .addScalar("num", StandardBasicTypes.LONG).list();
    
    0 讨论(0)
提交回复
热议问题