Reputation: 482
i have a hibernate class:
class student{
string name;
int roll;
}
while fetching records i have a requirement where i need to fetch records where name = "john" OR name = "paul", so basically i am trying to nget all records who have names as john or paul. I am not able to do the same. Please guide.
List students = sess.createCriteria(student.class)
.add( Restrictions.eq("name", "John") )
.add( Restrictions.eq("name", "Paul")) )
.list();
This is the non working code.
Upvotes: 2
Views: 226
Reputation: 18639
Something like this:
Criteria crit = session.createCriteria(Student.class);
Criterion name = Restrictions.eq("name", "John") ;
Criterion name2 = Restrictions.eq("name", "Paul"));
LogicalExpression orExp = Restrictions.or(name,name2);
crit.add(orExp);
List<Student> results = crit.list();
Upvotes: 1
Reputation: 12222
Try:
List students = sess.createCriteria(student.class)
.add( Restrictions.in("name", new String[] { "John", "Paul" } ) )
.list();
PS. I recommend you using QueryDSL than Criteria API.
Upvotes: 4