Hibernate query result Bar number set
Original wording:
Integer count = (integer) session.createquery (HQL). Uniqueresult ();
Error: Java.lang.ClassCastException:java.lang.Long cannot is cast to Java.lang.Integer
Reason:
After upgrading from Hibernate 3.0.x/3.1.x to the latest version 3.2, many of the 3.2 SQL functions such as COUNT (), the unique return value of sum () have changed from integer to Long, and if the code is not upgraded, Will get a classcastexception.
This change is primarily for JPA compatibility and can be found in the latest documentation for hibernate.org.
Solution:
1.hibernate is a solution in particular. When using Hibernate's query function for the value of count (), sum (), and so on ( Note: Must be only returned with a unique value and is available for number format ) uniqueresult () of query can be called; This method returns the object, simply converting it to the number type and then calling. Intvalue ().
For example:
Integer count = ((number) Session.createquery (HQL). Uniqueresult ()). Intvalue ();
2. You can convert long to a string and then convert the string to integer;
Long i = (long) session.createquery (HQL). Uniqueresult (); Integer IInew Integer (string.valueof (i));
These two solutions can solve such problems ~
Java.lang.ClassCastException:java.lang.Long cannot is cast to Java.lang.Integer