-->
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: HQL like clause
PostPosted: Fri Jul 03, 2009 4:48 am 
Newbie

Joined: Fri Jul 03, 2009 4:46 am
Posts: 3
Hello,
I've been trying to get Users with their lastnames beginning with a string parameter (alpha), but this request doesn't work :

Code:
public List<User> getAllOrderCreated(Intranet intranet, String alpha, int start, int max){
      if(intranet==null){
         return this.getSession().createQuery("from User where intranet is null and lastName like ?% order by createdAt desc").setParameter(0, alpha).setMaxResults(max).setFirstResult((start-1)*50).list();
      }
      else{
         return this.getSession().createQuery("from User where intranet=? and lastName like ?% order by createdAt desc").setParameter(0, intranet).setParameter(1, alpha).setMaxResults(max).setFirstResult((start-1)*50).list();
   
      }
   }


Any ideas ?


Top
 Profile  
 
 Post subject: Re: HQL like clause
PostPosted: Fri Jul 03, 2009 4:55 am 
Newbie

Joined: Fri Jul 03, 2009 4:46 am
Posts: 3
Ok, i found the solution (using concat):

Code:
public List<User> getAllOrderCreated(Intranet intranet, String alpha, int start, int max){
      if(intranet==null){
         return this.getSession().createQuery("from User where intranet is null and lastName like concat(?,'%') order by createdAt desc").setParameter(0, alpha).setMaxResults(max).setFirstResult((start-1)*50).list();
      }
      else{
         return this.getSession().createQuery("from User where intranet=? and lastName like concat(?,'%') order by createdAt desc").setParameter(0, intranet).setParameter(1, alpha).setMaxResults(max).setFirstResult((start-1)*50).list();
   
      }
   }


Top
 Profile  
 
 Post subject: Re: HQL like clause
PostPosted: Fri Jul 03, 2009 7:26 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
Maybe better would be this (?):

Code:
return this.getSession().createQuery("from User where intranet=? and lastName like ? order by createdAt desc").setParameter(0, intranet).setParameter(1, alpha+"%")


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.