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: Using like clause with list of string objects
PostPosted: Wed Feb 06, 2008 9:14 am 
Newbie

Joined: Wed Feb 06, 2008 9:02 am
Posts: 2
We have problem where we need to use like clause for array of strings e.g name

select * from xyz where name like '%ABC%';

Now here values will come in form of String Arrays

do we tried using

String[] a_Name; has some values

l_Query.setParameterList("Name",a_Name)

we tried using

select
r1
from
xyz r1
where
r1.Name like :Name

&

select
r1
from
xyz r1
where
r1.Name like ':Name'

It didnt work , we vene tried create Expression.like("Name",a_Name) but it gives Class cast exception as its expecting Object.


Any Solution.


Top
 Profile  
 
 Post subject: Re: Using like clause with list of string objects
PostPosted: Wed Feb 06, 2008 11:31 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
how do you want to put the values in the query? give me an example of what the query should look like if this array is passed:

String[] param = new String[]{"foo", "bar"}



Farzad-


Top
 Profile  
 
 Post subject: Use Criteria
PostPosted: Wed Feb 06, 2008 11:34 am 
Newbie

Joined: Mon Jul 09, 2007 10:48 am
Posts: 7
What about to use Criteria instead of HSQL? We use Criteria queries in 99% of cases


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 06, 2008 4:58 pm 
Beginner
Beginner

Joined: Mon Mar 07, 2005 6:23 pm
Posts: 21
I don't think you can use LIKE as you would an IN clause.

You probably need to do something like (in SQL terms)

select * from some_table where text_column like '%ABC%' OR text_column like '%DEF%' OR text_column like '%GHI%'

Though if you're doing a bunch of crazy text match queries, you might be better off using Hibernate Search (Lucene)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 06, 2008 6:27 pm 
Newbie

Joined: Wed Feb 06, 2008 5:52 pm
Posts: 4
Try using critera query
with "like" restriction in disjunctions
and you can iterate this for every parameter in the Array.

It will create a SQL like:

select * from.... where name like '% 1stString %'
or name like '% 2stString %'
or name like '% 3stString %'
....

this worked in my case..
hope it helps you also


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 07, 2008 7:57 am 
Newbie

Joined: Wed Feb 06, 2008 9:02 am
Posts: 2
Basically the query should be something like this

select * from xyz
where
Name like '%ABC%'
and Report_Date = (
select max(Report_Date) from
xyz
where
Name like '%ABC%'
)

where Name will be populated as a String []

String[] name = new String[]{"%ABC%", "%EFG
%", "%IRY%"}

we tried using

l_Query.setParameterList("Name",name) with namedquery as

select * from xyz
where
Name like :name
and Report_Date = (
select max(Report_Date) from
xyz
where
Name like :name
)

Its was giving ORA-00907: missing right parenthesis

Another way we tried using "like" restriction in disjunctions which will create the query as

select * from xyz
where
name like '%GEGW%' or name like '%BC07%' or name like '%BACF%' and Report_Date = (
select max(Report_Date) from
xyz
where
name like '%GEGW%' or name like '%BC07%' or name like '%BACF%'
);

This will not give the right data from sub query as it will give max of report_date among all names.

Based on above cases i might end up using a sp .

Any other solution using crietria queries or something else .

Thanks for all inputs till now


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.