Hibernate:java.lang.ClassCastException:java.lang.Integer无法转换为java.lang.Double
作者:互联网
我用Java定义的Staff实体是这样的:
final public class Staff {
private int staffId;
private String firstName = null;
private String lastName = null;
private String Email = null;
private double salary;
//and the setters and getters
}
我的查询代码:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Staff staff = null;
try {
Criteria criteria = session.createCriteria(dto.Staff.class);
criteria.add(Restrictions.eq("salary", 1000000));
staff = (Staff) criteria.uniqueResult();
} catch(Exception e) {
System.out.println("Error : " + e);
} finally {
session.close();
}
但是当我运行它时,我得到一个错误,它指出:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
如果我将数字显式转换为两倍,则可以使用:
criteria.add(Restrictions.eq("salary", (double) 1000000));
没有显式强制转换,有没有办法做到这一点?
我还认为在Java中,从Integer到Double的转换是隐式的吗?
解决方法:
Hibernate认为它是整数,告诉它是两倍.
criteria.add(Restrictions.eq("salary", 1000000d));
标签:classcastexception,casting,hibernate,java 来源: https://codeday.me/bug/20191030/1966034.html