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 conditionCode:
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]