Search code examples
grailscreatecriteria

Grails Create Critera and Listing single or specified column/s value only?


I grails i m trying to fetch a lookup value from the database and i want to list a list of single column value that is "Value" column .

    private Static Final String Custom = "Custom"  //lie in class Constants

    LookUp.createCriteria.list() { 

    eq('type',LookupTypeEnum.valueOf(Constants.Custom).toString())



   }

this listing was like select ,

how can i make this query into

   Select Value from LookUp where Type = 'Custom' 

i want my grails query to return me a single query result like the sql. I want to bind it to a list box?


Solution

  • My bad this do all the trick , private Static Final String Custom = "Custom" //lie in class Constants //LookupTypeEnum is Enum collection implementation class if don't have it simply you can replace it with your value ="Custom" or a variable Constants.Custom

      LookUp.createCriteria.list() { 
      eq('type',LookupTypeEnum.valueOf(Constants.Custom).toString())
      projections {  //projection does the trick
       property('value')
     }
    

    }

    It's equivalent SQl select query is :

    select value from lookup where type='custom' ;