-->
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: Intersect Workaround
PostPosted: Sun Jan 01, 2006 1:19 pm 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
I'm using hibernate 3 on mysql 5 to model a many to one of Multimedia to Tag.

I started with a search for Multimedia with any of the given tags - e.g.:
Code:
from Multimedia m where m.tags.tagValue in ('Tim', 'Sweden')


I would like to search for Multimedia with all of the given tags - e.g.:
Code:
from Multimedia m where m.tags.tagValue = 'Sweden' and m.tags.tagValue = 'Tim'


Obviously this is not good hql as the number of search tags is unknown. Given that there is no interect, can someone please suggest cleaner hql for this? I know I'm missing something simple...

Many thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 01, 2006 3:42 pm 
Newbie

Joined: Sun Dec 28, 2003 4:35 pm
Posts: 13
You could use a Criteria Query.

Chapter 15. Criteria Queries

Greetings, Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 01, 2006 4:32 pm 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Thanks mate - don't know what the hell I was thinking for not using them! Credits are yours...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 02, 2006 5:08 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Just looked at the criteria stuff - it's still going to have to have a loop to add

Code:
session.createCriteria(Multimedia.class)
  // for each tag loop doing
  .createCriteria("tags")
    .add( Restrictions.eq("tagValue", %LOOP_VALUE%) )


Is there a way to do this without looping?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 02, 2006 6:57 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Fixed now:

I didn't think "allEq" would work with a map with multiple values of the same key but it does

Code:
      Map tagsMap = new HashMap();
      tagsMap.put("tagValue","Sweden");
      tagsMap.put("tagValue","Tim");
      return getSession()
            .createCriteria(Multimedia.class)
               .createCriteria("tags")
                  .add(Restrictions.allEq(tagsMap))
            .list();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 7:53 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Seamingly this doesn't work after all and can return ID's 1,2,1,2.
I could add a results transformer but don't want to as it'll screw up pagination.

Can anyone suggest some HQL to do what was posted in the original message?
Thanks


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.