-->
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, setParameters and Wildcards !
PostPosted: Tue Mar 01, 2011 12:09 pm 
Newbie

Joined: Tue Mar 01, 2011 12:05 pm
Posts: 3
Hi,

I am trying to achive the following :

Code:
String queryString = "select o from Productso where (o.productCategory like '%:category%' and o.productName like '%:name%' and o.id.productCountry like '%:country%' )";
Query query = entityManager().createQuery(queryString);
query.setParameter("name",name);
query.setParameter("category",category);
query.setParameter("country",country);
query.getResultList();


But when using the "%" operator the setParameter Method doesn't seem to work ... The other way would be building the queryString with the parameters as a string (whoch works) and not using the "setparameter" method which i am trying to avoid ...


Top
 Profile  
 
 Post subject: Re: HQL, setParameters and Wildcards !
PostPosted: Tue Mar 01, 2011 12:19 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Include the % wildcards in the parameter values and you should be fine. Eg. query.setParameter("name","%" + name + "%");. Of course, you should remove them from the query string: ... where (o.productCategory like :category ...


Top
 Profile  
 
 Post subject: Re: HQL, setParameters and Wildcards !
PostPosted: Wed Mar 02, 2011 4:55 am 
Newbie

Joined: Tue Mar 01, 2011 12:05 pm
Posts: 3
Code:
      query.setParameter("category","'%" +category+ "%'");



Thanks very much .... my stupid errror was adding the ' into the Parameter, and of course then Hibernate escapes ...

Thanks ! sometimes you get routine-blinded ....


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.