Amitab Das
Amitab Das

Reputation: 55

Hibernate : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double

My Staff Entity defined in Java is like this :

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
}

My Query code :

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();
}

But when I run this I get an error which says :

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double

If I explicitly cast the number to a double it works :

criteria.add(Restrictions.eq("salary", (double) 1000000));

Is there any way to do this without the explicit casting? Also I thought that in Java, Integer to Double conversion was Implicit?

Upvotes: 3

Views: 14312

Answers (2)

Suresh Atta
Suresh Atta

Reputation: 122006

Hibernate considering it as a integer, tell that it's double.

    criteria.add(Restrictions.eq("salary",   1000000d));

Upvotes: 4

Scary Wombat
Scary Wombat

Reputation: 44854

The restriction.eq takes an object, so no implicit casting is possible.

Upvotes: 0

Related Questions