Domanda

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?

È stato utile?

Soluzione

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

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

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top