문제

I am using HibernateCriteriaBuilder api to write my Criteria Queries. I want to know if inside Criteria we can have conditional logic, such as an if statement?

For example:

 OnemonthList=it.createCriteria().list {   
   if (res_id!='all'){
        eq('graresource',resourceInstance)
   }         
    between('currentdate', fromDate, toDate)         
    projections {       
    trans {
      countDistinct('id')    
    }
    groupProperty('currentdate')
        }                  
    } 

Is this valid?

도움이 되었습니까?

해결책

Yes, you can use any sort of conditional or looping logic inside of the criteria DSL. Your example will work. Using loops can be incredibly useful, for example:

Domain.createCriteria().list {
    params.mapOfConditions.each {
        eq it.key, it.val
    }
}

will dynamically add an eq for each entry in the map that you have.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top