Java.lang.ClassCastException:Ljava.lang.Object; Cannot is cast to Com.entity.Advertisem exception

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.