-->
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: Converting HQL to Criteria Query
PostPosted: Wed Nov 07, 2007 2:07 am 
Newbie

Joined: Wed Dec 28, 2005 5:44 am
Posts: 12
Hi,

I have this simple HQL query.

Code:
String hqlQuery = "from Department d where d.deptId="+deptId+"";


I have few Criteria queries which is used for the search functionality.
Between these two dates it searches for the data. This part is working fine.

Code:
getSession().createCriteria(DepartmentBean.class).
add(Restrictions.between("createdDateTime",toDate,fromDate));


But now my problem is
1. To combine my hqlQuery with this criteria query so that there is an additional condition for search i.e, search will now depend on the deptId that's passed.

2.
Code:
Query query = getSession().createQuery(pass that consolidated query here);
return query.list();
 


Please advice.

Thanks,
MJ


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 07, 2007 3:23 am 
Newbie

Joined: Wed Dec 28, 2005 5:44 am
Posts: 12
I tried this




Code:

--------------------------------------------------------------------------------

  List<DepartmentBean> critList =  getSession().createCriteria(DepartmentBean.class).add(Restrictions. eq("deptId",deptId)).add(Restrictions.le("createdDateTime",toDate,fromDate)).list(); 

--------------------------------------------------------------------------------



But it is throwing this error

Code:

--------------------------------------------------------------------------------

java.lang.ClassCastException: java.lang.String   

--------------------------------------------------------------------------------






Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 07, 2007 8:45 am 
Newbie

Joined: Wed Dec 28, 2005 5:44 am
Posts: 12
I tried the following to compile my query -


Code:
Criteria critQuery = getSession().createCriteria(CallRequestBean.class).add(Expression.sql("from DepartmentBean db where db.deptId:=deptId "));
critQuery = critQuery.add(Restrictions.le("db.createdDateTime",toDate));
String strQuery = critQuery.toString().replaceAll(",","and");


Now strQuery is -
Code:
from Department db where db.deptId=585 and db.createdDateTime<=10/03/2007
Query query = getSession().createQuery(strQuery);


But this query is not running and throwing this error -
Code:
java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V


In one of the forums saw this was due to the missing antlr.jar but I dont think so and moreover I have the antlr.jar in my lib folder

Please Advice.

Thanks,
MJ


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.