-->
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.  [ 2 posts ] 
Author Message
 Post subject: search results using criteria
PostPosted: Tue Jun 12, 2007 11:15 am 
Newbie

Joined: Tue Jun 12, 2007 8:57 am
Posts: 1
I have a problem with sending a search request. i am using the criteria/criterion interfaces and restrictions.

Hibernate version:3.12

Code between sessionFactory.openSession() and session.close():

Name and version of the database you are using: mysql

i have two classes Phones and Featrues, an Phone has many Featrues like
"Video record","Slider design","Touchscreen" Etc. And every Featrue also has many Phones :


Code:

class Phones{
   //MonyToMany
   List<Fetrues> fetrues;
}

class Fetrues{
   String name; // example:  "Video record","Slider design","Touchscreen"
}



I want to find phones which fetrues contains with name= "Video record" and name ="Slider design"

i have three test data in database:


phone1: "Video record","Slider design","Touchscreen"

phone2: "Video record","Touchscreen"

phone3: "Slider design","Touchscreen"





Code:
Features f1 = new Features();
      f1.setName("Video record");
Features f2 = new Features();
      f2.setName("2 megapixel");

Disjunction d = Restrictions.disjunction();
      d.add(Example.create(f2)).add(Example.create(f1));
      
Conjunction c = Restrictions.conjunction();
      c.add(Example.create(f2)).add(Example.create(f1));



use Conjunction , result size is 0, Even if phone1's fetrues meets condition

Code:
List<Phones> result = getSession().createCriteria(Phones.class).createCriteria("features").add(c).list();


CriteriaImpl(com.mpr.model.entity.Phone:p[Subcriteria(features:kt)]
[(example (com.mpr.model.entity.Features@aaf063[id=<null>,name=2 megapixel,profile=<null>,image=<null>])
and example (com.mpr.model.entity.Features@8bf3ec[id=<null>,name=Video record,profile=<null>,image=<null>]))])


The follow way will products all Phones(Phone1,Phone2 and Phone3), but only want Phone1 ,
Can anybody tell me how to do ,thx!


Code:
getSession().createCriteria(Phones.class).createCriteria("features").add(d).list();


CriteriaImpl(com.mpr.model.entity.Phone:p[Subcriteria(features:kt)]
[(example (com.mpr.model.entity.Features@aaf063[id=<null>,name=2 megapixel,profile=<null>,image=<null>])
or example (com.mpr.model.entity.Features@8bf3ec[id=<null>,name=Video record,profile=<null>,image=<null>]))])[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 12, 2007 11:53 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Ignore criteria for now and think of the SQL you are trying to write.

What you've asked in your disjunction is give me the phones that have a feature with this name OR that name.

With your conjunction you've asked for phones that have a feature with this name AND that name. i.e. a feature that has 2 names that matches your criteria. Or, in other terms, you're asking for the impossible since your features can only ever have 1 name at any given time.

Write the SQL first and take Hibernate out of the equation, once you have that you then have a chance to get it right. However, I'm thinking your going to need two separate joins to your features table and AFAIK that still isn't possible with Criteria API.


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