-->
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: sql-query using like
PostPosted: Wed Nov 29, 2006 12:16 pm 
Newbie

Joined: Fri May 27, 2005 1:53 pm
Posts: 3
Ia m using hibernate 3.1 and was wondering how to use a like statement in a sql-query when loading a collection. I have the key set up but I don't want it to have to match exactly. Please let me know if I am not clear. Thanks.


<set name="laboratories" inverse="true" lazy="false">
<key property-ref="indicOrganism">
<column name="indic_organisms" not-null="true" />
</key>
<one-to-many class="com.yum.fsraqa.model.Laboratory" />
<loader query-ref="labs" />
</set>

<sql-query name="labs">
<load-collection alias="lab" role="com.yum.fsraqa.model.IndicOrganism.laboratories" lock-mode="read" />
SELECT {lab.*} FROM labratory lab WHERE lab.indic_organisms like :indicOrganism
</sql-query>

I am not sure where to add the %% so that the select will return all results like %indicOrganism%

Thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 12:53 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Yes you have to add the %'s

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 7:37 am 
Beginner
Beginner

Joined: Tue Nov 21, 2006 5:18 am
Posts: 31
Location: Bangalore, India
Hi,

You have to append the % before and after the :indicOrganism variable.

that means if your search is like

indicOrganism = test

then you should pass the indicOrganism like %test%

it will work

_________________
persist_coder
--credit please if it helps you


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 01, 2006 4:13 pm 
Newbie

Joined: Fri May 27, 2005 1:53 pm
Posts: 3
I understand that I need the % in the query but I am trying to use this in an automatic loading collection. I am not running any of this in "code" it is all being done on initialization of the class by hibernate.

If I didn't need the like part of the statement, everything would work fine, but since I am not simply using an =, the collection does not get populated correctly.

Does what I want to do make sense?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 5:59 am 
Beginner
Beginner

Joined: Tue Nov 21, 2006 5:18 am
Posts: 31
Location: Bangalore, India
Hi,

I understood that you have Collection and you want to apply wildcard for all the object in the collection,

You can iterate the collection and append the % before and after. now the collection contains with wild card. you can use it directly like below


List l = new ArrayList();
String s = "%arsh %";
l.add(s);
s = "%avid% ";
l.add(s);
Query q = session.createQuery(" from DefectDetails d where d.Assignedto = :assign");
q.setParameterList("assign", l);

It will work

_________________
persist_coder
--credit please if it helps you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 11:30 am 
Newbie

Joined: Fri May 27, 2005 1:53 pm
Posts: 3
I know its been a wheil since i posted a resolution, but none of these answers work.

Let me explain a little more about what I am trying to do. I want to have hibernate automatically load the set:

<set name="laboratories" inverse="true" lazy="false">
<key property-ref="indicOrganism">
<column name="indic_organisms" not-null="true" />
</key>
<one-to-many class="com.yum.fsraqa.model.Laboratory" />
<loader query-ref="labs" />
</set>

and it is a many to many relatinship, the Laborator class has a field indicOrganisms that is a list of names deliminated by "-", ex. "One-Two-Three"

I want the IdicOrganism to load any Lab that has its name in this field, i.e. IndicOrganism "One" should load all Labs with "One", "One-Two", etc.

I am trying to use this sql-query
<sql-query name="labs">
<load-collection alias="lab" role="com.yum.fsraqa.model.IndicOrganism.laboratories" lock-mode="read" />
SELECT {lab.*} FROM laboratory lab WHERE lab.indic_organisms like :indicOrganism
</sql-query>

when I add the "%" around the :indicOrganism I get the error,
Line 1: Incorrect syntax near '@P0'


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.