-->
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.  [ 6 posts ] 
Author Message
 Post subject: Escaping '%' in like clauses
PostPosted: Tue May 04, 2004 10:01 am 
Newbie

Joined: Thu Aug 28, 2003 6:03 am
Posts: 7
Using Hibernate 2.1.2 against Oracle 9. It's a generic problem so I haven't included mappings yet...

I want to construct a like clause in OQL to retrieve rows where the value in a field starts with the percent character. In JDBC, I would write this:

select * from table_name where field_name like '&%%' {escape '&'}

So I tried this in OQL:

select b1 from b1 in class test.Blah where b1.name like '&%%' {escape '&'}

But this gets translated by Hibernate into this:

select b1.id as x0_0_ from djones.blah b1 where (b1.name like '&%%' {escape '&' } )

This is not a valid JDBC SQL statement.

I think the problem may be related to a bug with named SQL queries on JIRA:

http://opensource.atlassian.com/project ... wse/HB-898

But thought I'd ask here if there is a different way to formulate this particular LIKE query in OQL.

Thanks in advance,

DJ


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 10:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
That is not a bug. Nowhere in the Hibernate documentation does it say that you can use JDBC escapes in HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 10:49 am 
Newbie

Joined: Thu Aug 28, 2003 6:03 am
Posts: 7
Thanks for the quick response. (Sorry - mixed up OQL and HQL in original message but you got my gist!)

I can't figure out from the docs how to formulate the "begins with percent" like clause in HQL. Is there a way?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 9:15 pm 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
this works fine for me... (oracle)

Code:
if (lastName != null && lastName.length()>0)
            buffy.append(" and patient.lastname like'").append(lastName.toUpperCase()).append("%'");
         


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 05, 2004 3:44 am 
Newbie

Joined: Thu Aug 28, 2003 6:03 am
Posts: 7
Thanks, but that didn't work for me...

Here are the Oracle statements to create my trivial test table and insert some values:

create table blah (name varchar2(255) not null);
insert into blah values ('% profit');
insert into blah values ('% yield');
insert into blah values ('diff');
commit;

In my test app, I want to pull out just the rows beginning with the percent character, but SQL assumes that a percent in a like clause is a multi-character wildcard unless it is escaped. I can do this in SQL using the 'escape' keyword (in Oracle anyway):

select name from blah where name like '&%%' escape '&';

...but I'm using Hibernate and need to do this with HQL.

Any more ideas?

DJ


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 05, 2004 9:23 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
Sorry. I finally understand the problem... have not had to handle this situation. I wonder is session.createSQLQuery could help get around the problem...


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