I'm trying to use a CASE
statement to order by GORM query. What I have is a view with a column of state
the value there can be a state abbreviation or the word General
. I will run the query like this
Dropdown.findByStateInList(['General','CA'], [sort: "stateOrderBy", order: "asc"]
but it returns an error of
could not resolve property: stateOrderBy of: workspace.Dropdown
When I create my domain class like below the value of stateOrderBy
is the string CASE WHEN state = 'General' THEN 2 ELSE 1 END
and not a 1 or 2 which is why I'm getting the error above. Is there a way to evaluate my CASE statement?
@EqualsAndHashCode(includeFields=true)
class Dropdown implements Serializable {
String state
String dropDownNames
String sectionName
String prefix
Integer displayOrder
def stateOrderBy = "CASE WHEN state = 'General' THEN 2 ELSE 1 END"
static transients = [ "stateOrderBy" ]
static mapping = {
datasource 'plDropdown'
table 'view_AllTables'
id composite: ['state','sectionName','displayOrder']
state column:'state'
dropDownNames column:'DropDownNames'
sectionName column:'SectionName'
prefix column:'Prefix'
displayOrder column:'DisplayOrder'
stateOrderBy column:'stateOrderBy'
version false
}
}
EDIT: There will always be a result in the General
column, but there may not be a result for the state specific query. If there is a state specific answer returned I want to use that otherwise the General answer.