-->
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: Using wildcards with named parameters
PostPosted: Thu Jun 28, 2007 8:36 am 
Newbie

Joined: Mon Sep 04, 2006 1:27 pm
Posts: 9
Hi,could somebody put an example of how to use a wildcard % with named parameters,I know do it in a SQLQuery
String sql = "select * from tablename where fieldname1 like ? ;
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(classname.class);
query.setString(0,"%"+fieldname1+"%");
that's work fine
but I want to do it with named paramaters,where we have to put the % wildcard?
String queryString = "from " + classname.class.getName() + " e where e.nombre like :nombre ";
List list = hibernateTemplate.findByNamedParam(queryString,"nombre", nombre);
return list;
}

If we put the widcards inside the String of the query eg where e.field like :nombre%";
throws an unexpected '%'
Also I tested put in hibernateTemplate.findByNamedParam(queryString,"%nombre%", nombre);
throws an cannot locate named parameter [%nombre%].

Can we use wildcards with hibernate named parameters??


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 11:15 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
This should be something like:

String hql = "from " + classname.class.getName() + " e where e.nombre like :nombre ";
Query query = session.createQuery(sql);
query.setString("nombre","%"+fieldname1+"%");

Hope this helps.

_________________
dont forget to rate !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 02, 2007 8:23 pm 
Newbie

Joined: Mon Sep 04, 2006 1:27 pm
Posts: 9
Thanks steckemetz,I knew how to do it with createSQLQuery and createQuery, I wrote the code in my msg very similary to your code but with an createSQLQuery method using an ?.
thanks to your answer I got the idea in a method findByNamedParam of spring hibernateTemplate:
String queryString = "from " + class.class.getName() + " e where e.nombre like :nombre ";
String orderByPart = "";
orderByPart += " order by e.iafiliado";
queryString += orderByPart;
List list = hibernateTemplate.findByNamedParam(queryString,"nombre", "%"+nombre+"%");
return list;
Your answer help me ,Thanks.


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.