biginteger和long精度_Hibernate返回BigIntegers而不是long

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

解决方案

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值