0

I need to fetch the result of the following query but i am getting a typecast exception. Kindly help out!

SELECT COUNT(*) FROM ( SELECT DISTINCT a.PROPSTAT_CODE,a.PROPSTAT_DESC,a.PROPSTAT_TYPE FROM CNFGTR_PROPSTAT_MSTR a WHERE 1 = 1 )

My code is given below,

Query query = session.createSQLQuery(sqlQuery);  

listRes = query.list();

int ans = ((Integer)listRes.get(0)).intValue();

Thanks in advance

6
  • why do you need to cast it to Integer first and then do a .intvalue()? Commented Apr 28, 2014 at 10:20
  • i found it as a solution on a site.Please suggest the right code. Commented Apr 28, 2014 at 10:22
  • You are trying to fetch multiple values from your query.. So I am guessing it might return a List of Maps.. Can you confirm this? Commented Apr 28, 2014 at 10:30
  • No,i am trying to get a count on the inner query so it will return exactly one integer value. Commented Apr 28, 2014 at 10:34
  • Thats alright,so do u have a solution ? Commented Apr 28, 2014 at 10:38

4 Answers 4

4

Since you say that you are wrapping the above query in another query that returns the count, then this will give you want, without having to convert to any other data types.

    Integer count = (Integer) session.createSQLQuery("select count(*) as num_results from (SELECT DISTINCT a.PROPSTAT_CODE,a.PROPSTAT_DESC,a.PROPSTAT_TYPE FROM CNFGTR_PROPSTAT_MSTR a WHERE 1 = 1)")
        .addScalar("num_results", new IntegerType())
        .uniqueResult();
    System.err.println(count);

The trick is the call to "addScalar". This tells Hibernate you want the data type of "num_results" pre-converted to an Integer, regardless of what your specific DB implementation or JDBC driver prefers. Without this, Hibernate will use the type preferred by the JDBC driver, which explains why different answers here have different casts. Setting the desired result type specifically removes all guesswork about your returned data type, gives you the correct results, and has the added bonus of being more portable, should you ever wish to run your application against a different relational database. If you make the call to "list" instead of "uniqueResult" then you can assign the results directly to a List

Sign up to request clarification or add additional context in comments.

Comments

1

Use long instead of int. Hibernate returns count(*) as long not int.

Query query = session.createSQLQuery(sqlQuery);  

listRes = query.list();

long ans = (long)listRes.get(0);

5 Comments

Did you try exactly like, long ans = (long)listRes.get(0); ?
,java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer is the exception.. in case you try to cast to long.. you will get an exception with Integer replaced by Long
Have finally figured it out guys,direct conversion isn't possible. First it needs to be converted into String and then into the required datatype. Integer.parseInt((listRes.get(0).toString)))
yup.. that would be one way too.. but the BigDecimal thing did not work for you? did you try that out?
Before doing all those conversion, try to understand what class it is actually returning. You can find out by System.out.println(listRes.get(0).getClass());
1

Well.. I suppose this should work:

Query query = session.createSQLQuery(sqlQuery);  

List listRes = query.list();

int ans = ((BigDecimal) listRes.get(0)).intValue();

Note: you need to import java.math.BigDecimal

1 Comment

No,Still the exception persists.Here is it,java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Integer
1
List number=session.createSQLQuery("SELECT COUNT(*) FROM devicemaster WHERE ClientId="+id).list();
session.getTransaction().commit();
int ans = ((java.math.BigInteger) number.get(0)).intValue();

1 Comment

While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.