我用 Java 定义的员工实体是这样的:
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 中,整数到双精度转换是隐式的?