-->
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.  [ 5 posts ] 
Author Message
 Post subject: complex criteria query
PostPosted: Tue Feb 14, 2006 3:41 pm 
Newbie

Joined: Wed Jan 18, 2006 2:19 pm
Posts: 14
Hi, I have many-to-many association between Book and KeyWord. I can find all Books written by given list of authors with the criteria query
Code:
Criteria criteria=session.createCriteria(Book.class);
criteria.createAlias("keyWords", "kw" );
criteria.add(org.hibernate.criterion.Expression.in("kw.word", authors))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);


But now, i want to replace the condiditon
Code:
word=author[0] or word=author[1]....

with
Code:
word like '%author[0]%' or word like '%author[1]%' ...


I can't imagine how to write this query at all :( Could you please help me?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 5:13 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Code:
Criteria criteria = session.createCriteria(Book.class).createCriteria("keyWords");
for (String author : authors)
{
  criteria.add(Restrictions.like("word", author, MatchMode.ANYWHERE));
}
// Etc.
You might prefer Restrictions.ilike for case insensitive matching, too.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 6:36 am 
Newbie

Joined: Wed Jan 18, 2006 2:19 pm
Posts: 14
First, I'm sorry that I mixed authors and keywords together, so I wonder you understanded what I mean. The problem is that adding a new criteria for each condition puts "and" operator between the conditions.
So the result sql query contains
Code:
word like something AND word like somethingelse

but i need
Code:
word like something OR word like somethingelse


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 10:39 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
there is an OR method in the Restrictions class. Just method chain it together (disjunction works as well)

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 2:52 pm 
Beginner
Beginner

Joined: Thu Jun 30, 2005 5:07 am
Posts: 33
look at my question, I need the "And" not the "Or" but only the "Or" is working so you can use it http://forum.hibernate.org/viewtopic.php?t=955442


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