Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
up vote 3 down vote accepted

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

    criteria.add(Restrictions.eq("salary",   1000000d));
share|improve this answer
    
But I haven't used Wrapper classes anywhere – Amitab Das Nov 15 '13 at 5:50
    
Is there any way I could add the 'd' to the number dynamically? Because I am trying to write a function which is supposed to set any type of restriction. In fact it could be an integer or a string. Is there any way to do it? – Amitab Das Nov 15 '13 at 5:58
    
Yes. You could write new Double(xxx.toString()) - this will do the right thing whether xxx is a Double, an Integer, a String or basically anything that can reasonably be converted to a Double. – David Wallace Nov 15 '13 at 7:26

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.