-->
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: MatchMode.ANYWHERE
PostPosted: Mon Dec 20, 2004 7:25 pm 
Newbie

Joined: Mon Dec 20, 2004 7:14 pm
Posts: 3
Hibernate version: 2.1.7

Hiya, I'm trying to get my head around Hibernate's HQL, and a few questions come to mind. The first is how I would achieve a where clause like this: "...where foo like '%bar%' " BUT without doing code like this:

add(Expression.like("foo", text, MatchMode.ANYWHERE))

I'd like to generate dynamic queries via string if possible, and syntax like %?% in the string doesn't seem to work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 5:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what you are showing is Hibernate's Criteria API - not HQL.

Look at HQL if you want to build up strings instead (but please use parameters on your query instead of 100% string building since its safer (avoid sql injection))

btw.

add(Expression.like("foo", text)) will also do the trick.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 12:26 pm 
Newbie

Joined: Mon Dec 20, 2004 7:14 pm
Posts: 3
max wrote:
what you are showing is Hibernate's Criteria API - not HQL.

Look at HQL if you want to build up strings instead (but please use parameters on your query instead of 100% string building since its safer (avoid sql injection))

btw.

add(Expression.like("foo", text)) will also do the trick.


Ah, thanks, that clears some stuff up. I'm trying to absorb all this plus Spring, and I missed that distinction along the way. I gather these two are distinct from Hibernate's QBE, as well.

I also realized how simple what I wanted to do is - just put the %'s in the string when doing the set, like so: setString("namedParm", "%" + text + "%"). It always seems simple when you know the answer...

I wanted to avoid the add(), Expression.or() and Expression.like() methods if possible, since I'm most likely doing or's for the dynamic portions of query and it quickly becomes unwieldy to have the nested like()'s inside the the or()'s, inside yet another or(), if you know what I mean.

Thanks again.


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.