Java Hibernate 返回 BigIntegers 而不是 longs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18758347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate returns BigIntegers instead of longs
提问by Tristan Van Poucke
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
错误:org.hibernate.property.BasicPropertyAccessor - HHH000123:IllegalArgumentException 类:be.gimme.persistence.entities.Sender,属性的 setter 方法:senderId
错误:org.hibernate.property.BasicPropertyAccessor - HHH000091:预期类型:long,实际值: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).
发生这种情况是因为 Sender 类中的 senderId 实际上是一个 long 而不是 BigInteger(由 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)?
我想知道在这种情况下最好的做法是什么,我应该使用 BigIntegers 作为 id 吗(似乎有点矫枉过正)?
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 BigInteger
s? Or any other ideas?
我应该手动将查询结果转换为类 Sender 的对象吗(那太可惜了)?或者我可以让 Hibernate 返回long
id 而不是BigInteger
s?还是有其他想法?
I'm using Spring, Hibernate 4.1.1 and MySQL
我正在使用 Spring、Hibernate 4.1.1 和 MySQL
采纳答案by rogerdpack
The default for ".list()" in hibernate appears to be BigInteger return types for Numeric. Here's one work around:
hibernate 中“.list()”的默认值似乎是 Numeric 的 BigInteger 返回类型。这是一种解决方法:
session.createSQLQuery("select column as num from table")
.addScalar("num", StandardBasicTypes.LONG).list();
回答by erencan
Object database mapping is wrong. There is a casting exception here saying database field is BigInteger
, but object property is long
.
对象数据库映射错误。这里有一个强制转换异常说数据库字段是BigInteger
,但对象属性是long
。
BigInteger
is a special class to hold unlimited size integer values. Furthermore, BigInteger
can not cast to long implicitly.
BigInteger
是一个特殊的类,用于保存无限大小的整数值。此外,BigInteger
不能隐式转换为 long。
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.
为避免此错误,BigInteger
应更改为long
兼容类型的数据库字段。将其更改为int
可以long
隐式转换为 int的类型。请参阅BigInteger。
回答by haripcce
you can check on following link check here for BigInteger identity generator class
您可以在此处查看 BigInteger 身份生成器类的以下链接 检查
回答by Crozeta
In older versions of Hibernate you can use
在旧版本的 Hibernate 中,您可以使用
session.createSQLQuery("select column as num from table")
.addScalar("num", Hibernate.LONG).list();
回答by Алексей Виноградов
Adding to #Hedley comment to fix it globally you can add a line in SQLDialect constructor. In my project it was like:
添加到#Hedley 注释以在全局范围内修复它,您可以在 SQLDialect 构造函数中添加一行。在我的项目中,它是这样的:
public PostgreSQLDialect() {
super();
registerHibernateType(Types.BIGINT, StandardBasicTypes.LONG.getName());
}