-->
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.  [ 4 posts ] 
Author Message
 Post subject: wildcard "%" (in like) not working in placeholders
PostPosted: Wed Aug 22, 2007 7:40 am 
Newbie

Joined: Fri Aug 17, 2007 5:45 am
Posts: 6
Hibernate version: 1.3.1

Name and version of the database you are using: Oracle 10i


Hello,

I have the next callbak DAO method:


Code:
    public Vmenstrans[] findByCodnt(
            final String startdate,
            final String enddate,
            final String codInt)
    throws VmenstransDaoException {
        Collection collection = (Collection) this.hibernateTemplate.execute(
                new HibernateCallback() {
                    public Object doInHibernate(Session session){
                        Query query = session.createQuery(" from Vmenstrans " +
                                " where timestamp >= to_date(?, 'dd/mm/yy HH24:MI') " +
                                " and timestamp <= to_date(?, 'dd/mm/yy HH24:MI')  " +
                                " and upper(codint) like ? "   +
                        " order by msgid desc ");
                        query.setCacheable(true);                       
                        query.setString(0, startdate);
                        query.setString(1, enddate);
                        query.setString(2, codInt); 
                        return query.list();                       
                    }
                });
        Vmenstrans ret[] = convertToDTO( (List) collection );
        return ret;           
    }



The problem comes in the like ? part of the sentence, when the placeholder replaces the variable I'm not getting the expected effect of having something like: "like %cat%"

I have tried adding;
" and upper(codint) like %?% "
or
" and upper(codint) like \'%?%\' "

but it does not work.


How can I add the wildcard % to my placeholder query.

Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 22, 2007 11:22 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
this is how I have done it in the past:

Code:
... like '%' || ? || '%'

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 5:28 am 
Newbie

Joined: Fri Aug 17, 2007 5:45 am
Posts: 6
Thanks. that solved the problem


Top
 Profile  
 
 Post subject: No, it's not a good idea.
PostPosted: Mon Apr 21, 2008 1:31 am 
Newbie

Joined: Fri Sep 16, 2005 3:08 am
Posts: 15
The best way to do it is like this:

" and upper(codint) like ? "

and:

query.setParameter(n, '%' + codint.toUpperCase() + '%');

Because it gives more flexibility and control of the pattern.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.