这是我的发件人实体
@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; } }
当我尝试执行以下查询时:
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();
发生以下错误:
错误:org.hibernate.property.BasicPropertyAccessor- HHH000123:类中的IllegalArgumentException:be.gimme.persistence.entities.Sender,属性的设置方法:senderId 错误:org.hibernate.property.BasicPropertyAccessor- HHH000091:期望的类型:长,实际值:java.math.BigInteger
错误:org.hibernate.property.BasicPropertyAccessor- HHH000123:类中的IllegalArgumentException:be.gimme.persistence.entities.Sender,属性的设置方法:senderId
错误:org.hibernate.property.BasicPropertyAccessor- HHH000091:期望的类型:长,实际值:java.math.BigInteger
发生这种情况是因为Sender类中的senderId实际上是long而不是BigInteger(由Hibernate返回)。
我想知道在这种情况下的最佳实践是什么,我是否应该将BigIntegers用作id(似乎有点过大)?
我应该手动将查询结果转换为Sender类的对象吗(那太可惜了)?还是可以让Hibernate返回longid而不是BigIntegers?还是其他想法?
long
BigInteger
我正在使用Spring,Hibernate 4.1.1和MySQL
hibernate中“ .list()”的默认值似乎是“数字”的BigInteger返回类型。解决方法:
session.createSQLQuery("select column as num from table") .addScalar("num", StandardBasicTypes.LONG).list();