簡體   English   中英

或條件-休眠

[英]Or criteria - Hibernate

我有一個休眠課程:

class student{
string name;
int roll;
}

在獲取記錄時,我有一個要求,即我需要獲取name =“ john”或name =“ paul”的記錄,因此,基本上,我正在嘗試獲取所有名稱為john或paul的記錄。 我不能做同樣的事情。 請指導。

List students = sess.createCriteria(student.class)
    .add( Restrictions.eq("name", "John") )
    .add( Restrictions.eq("name", "Paul")) )
    .list();

這是無效代碼。

嘗試:

List students = sess.createCriteria(student.class)
   .add( Restrictions.in("name", new String[] { "John", "Paul" } ) )
   .list();

PS。 我建議您使用QueryDSL而不是Criteria API。

像這樣:

 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM