These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to do with ClassCastException
PostPosted: Tue Aug 21, 2007 6:49 am 
Newbie

Joined: Tue Aug 21, 2007 6:14 am
Posts: 2
hello,everyone.I'm programming with Hibernate Framework for two month.Today I come up to a strange question.When I used session.createQuery(hql) to get data ,it works well.However , When I used session.createQuery(sql) to get data , an ClassCastException occured.How does it happen?What should I do?

The code:(The last line throws the excetption)
Code:
      hql = "SELECT a.* FROM business.tb_gl_userInf a WHERE  (a.registerType =4 "+ "AND a.registerTime BETWEEN '" + yesterday + "' AND '"+ today + "') OR (" + "a.mulRegTime BETWEEN '" + yesterday+ "' AND '" + today + "')";

      log.info(hql);

   List<TbGlUserInf> lsa = session.createSQLQuery(hql).list();
         
   log.info(lsa.size());
         
   Iterator<TbGlUserInf> itu = lsa.iterator();
   while (itu.hasNext()) {
      TbGlUserInf tu = itu.next();


The following is OK:
Code:
      hql = "FROM TbGlUserInf a WHERE (a.registerType =4 "+ "AND a.registerTime BETWEEN '" + yesterday + "' AND '"+ today + "') OR (" + "a.mulRegTime BETWEEN '" + yesterday+ "' AND '" + today + "')";

   log.info(hql);

   List<TbGlUserInf> lsa = session.createQuery(hql).list();
   log.info(lsa.size());

   Iterator<TbGlUserInf> itu = lsa.iterator();
   while (itu.hasNext()) {
      TbGlUserInf tu = itu.next();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 9:07 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
A query like that will return an Object[], not a TbGlUserInf. Hence the class cast exception.

Try this:
session.createSQLQuery(sql).addEntity(TbGlUserInf.class).list()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 9:56 pm 
Newbie

Joined: Tue Aug 21, 2007 6:14 am
Posts: 2
thatmikewilliams,that problem was solved in the way.Thank you.You are so excellent.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.