Today accidentally encountered such a problem, never met before, in the online search a lot of ways, thinking right, but still have to change according to their own procedures.
The first write is the HQL statement to the statistics of each month's data, but tried for a long time, the program has been reminding HQL statement exception, no way, can only modify the query statement, changed to a SQL statement for statistics.
The error arose, and it was out of hand:
Java.lang.ClassCastException:Ljava.lang.Object; Cannot is cast to com.entity.Advertisem this exception solution is very simple, first of all to understand
the difference between Createsqlquery and CreateQuery in Hibernate:
The former uses the HQL statement to query, the latter can use the SQL statement query
The former takes the bean generated by hibernate as the object to load the list back
The latter is stored as an array of objects
So it's not very convenient to use createsqlquery and sometimes want to load the list back with hibernate-generated beans.
But Createsqlquery has a way to convert objects directly
Query query = session.createsqlquery (SQL). Addentity (Xxxxxxx.class)
XXXXXXX represents the object of the bean generated by hibernate, which is the bean that the data table maps out
HQL statement: Statistics on each month's data
String hql = "Select sum (sto_sum), sum (sto_sumprice), sum (sto_salenum), sum (sto_salenum*sto_saleprice) from Stock WHERE Period_diff (Date_format (now (), '%y%m '), Date_format (adddate, '%y%m ')) = 1 ";
Query query = session.createsqlquery (HQL). addentity (Stock.class);//Key steps
List = Query.list ();
So the problem is rooted in the type mismatch, can not be converted, the problem is solved!!!
Java.lang.ClassCastException:Ljava.lang.Object; Cannot is cast to Com.entity.Advertisem exception