Hibernate returns BigIntegers instead of longs

喜夏-厌秋 提交于 2019-12-30 08:00:23

问题


This is my Sender entity

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


...

    public long getSenderId() {
            return senderId;
    }

    public void setSenderId(long senderId) {
            this.senderId = senderId;
    }
}

When I try to execute following query:

StringBuilder query = new StringBuilder();
query.append("Select sender.* ");
query.append("From sender ");
query.append("INNER JOIN coupledsender_subscriber ");
query.append("ON coupledsender_subscriber.Sender_senderId = sender.SenderId ");
query.append("WHERE coupledsender_subscriber.Subscriber_subscriberId = ? ");

SQLQuery q = (SQLQuery) sessionFactory.getCurrentSession().createSQLQuery(query.toString());
q.setResultTransformer(Transformers.aliasToBean(Sender.class));
q.setLong(0, subscriberId);

return q.list();

The following error occures:

ERROR: org.hibernate.property.BasicPropertyAccessor - HHH000123: IllegalArgumentException in class: be.gimme.persistence.entities.Sender, setter method of property: senderId

ERROR: org.hibernate.property.BasicPropertyAccessor - HHH000091: Expected type: long, actual value: java.math.BigInteger

This happens because the senderId in the class Sender is actually a long instead of a BigInteger (which is returned by Hibernate).

I was wondering what the best practice was in a case like this, should I be using BigIntegers as id's (Seems a bit of an overkill)?

Should I convert the query results to objects of class Sender manually (That would be a pitty)? Or can I just make Hibernate return long id's instead of BigIntegers? Or any other ideas?

I'm using Spring, Hibernate 4.1.1 and MySQL


回答1:


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();



回答2:


In older versions of Hibernate you can use

  session.createSQLQuery("select column as num from table")
 .addScalar("num", Hibernate.LONG).list();



回答3:


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());
    }



回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/18758347/hibernate-returns-bigintegers-instead-of-longs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!