-->
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.  [ 7 posts ] 
Author Message
 Post subject: combining search results
PostPosted: Thu May 24, 2007 8:41 am 
Newbie

Joined: Thu May 24, 2007 8:28 am
Posts: 7
I have a problem with sending a search request. i am using the criteria/criterion interfaces and restrictions.

Hibernate version:3

Name and version of the database you are using:Oracle9

i have to classes Image and Keyword, an Image has many Keywords, every Keyword has one Image:



Code:
@Entity
@Table(name = "IMAGES")
public class Image{
private Set<Keyword> keywords;
private String name;

   public String getName() {
      return name;
   }
   @OneToMany
   public Set<Keyword> getKeywords() {
      return keywords;
   }
   public void setKeywords(Set<Keyword> keywords) {
      this.keywords = keywords;
   }
   public void setName(String name) {
      this.name = name;
}


Code:
@Entity
@Table(name="KEYWORDS")
public class Keyword{
   private String keywordname;
   public String getKeywordname() {
      return keywordname;
   }
   public void setKeywordname(String keyword)  {
      this.keywordname = keyword;
   }
}


I want a searchcriterion for example find all images with name = xyz and which contains keywords with name =abc

the seperate search would be easy:
Code:
List a = handler.getSession().createCriteria(Image.class)
         .createCriteria("keywords")
         .add(Restrictions.like("keywordname", "abc", MatchMode.ANYWHERE)).list() ;

and
Code:
List b = handler.getSession().createCriteria.add(Restrictions.like("name","xyz",MatchMode.ANYWHERE)).list() ;

but how can i combine them? restrictions.and needs two Criterions but the first one gives me a criteria
mfg
adonay[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 8:56 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Code:
List a = sess.createCriteria(Image.class).add(Restrictions.like("name", "xyz", MatchMode.ANYWHERE))
        .createCriteria("keywords")
        .add(Restrictions.like("keywordname", "abc", MatchMode.ANYWHERE)).list() ;



Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 9:11 am 
Newbie

Joined: Thu May 24, 2007 8:28 am
Posts: 7
ok sorry wasn't able to formulate the real problem.... i have several criterias (like the keyword one) and should combine them with and or or. and is no prblem but the or seems not that easy that way


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 9:21 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

is this what you want

Code:
List a = sess.createCriteria(Image.class,"i")
        .createCriteria("keywords","k")
        .add(Restrictions.or(Restrictions.like("i.name", "xyz", MatchMode.ANYWHERE) ,Restrictions.like("k.keywordname", "abc", MatchMode.ANYWHERE))).list();



You didn't rate


Amila

(Don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 9:58 am 
Newbie

Joined: Thu May 24, 2007 8:28 am
Posts: 7
to pull out this topic again:
i have now 3 different classes: images, keywords and albums, an image has many keywords and can be found in many albums, how combine them?

Code:
      criteria = session
      .createCriteria(Image.class,"i")
      .createCriteria("keywords", "k")
                                     .createCriteria("albums","a)
      

does not work:

Exception in thread "AWT-EventQueue-0" org.hibernate.QueryException: could not resolve property: albums of: source.model.Keyword

mfg
adonay


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 10:13 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi


criteria = session
.createCriteria(Image.class,"i")
.createCriteria("keywords", "k")
.createCriteria("i.albums","a)

I think your album collection in Image class. just address property with its own alias.


Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 10:18 am 
Newbie

Joined: Thu May 24, 2007 8:28 am
Posts: 7
thanks that was exactly the thing i was looking for, will try it out


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