0

I found the solution Burt Beckwith offered in this question: add user define properties to a domain class and think this could be a viable option for us in some situations. In testing this, I have a domain with a Map property as described in the referenced question. Here is a simplified version (I have more non-map properties, but they are not relevant to this question):

class Space {
    String spaceDescription
    String spaceType
    Map dynForm  

     String toString() {
    return (!spaceType)?id:spaceType + " (" + spaceDescription + ")"
 }  
}

I have some instances of space saved with some arbitrary data in the dynForm Map like 'Test1':'abc' and 'Test2':'xyz'.

I am trying to query this data and have succesfully used HQL to filter doing the following:

String className = "Space"
Class clazz = grailsApplication.domainClasses.find { it.clazz.simpleName == className }.clazz

def res = clazz.executeQuery("select distinct space.id, space.spaceDescription from Space as space  where space.dynForm['Test1'] = 'abc'  " )  
log.debug "" +  res

I want to know if there is a way to select an individual item from the Map dynForm in a select statement. Somehting like this:

def res = clazz.executeQuery("select distinct space.id, elements(space.dynForm['Test1']) from Space as space  where space.dynForm['Test1'] = 'abc'  " )

I can select the entire map like this:

def res = clazz.executeQuery("select distinct elements(space.dynForm) from Space as space  where space.dynForm['Test1'] = 'abc' " )

But I just want to get a specific instance based on the string idx.

Community
  • 1
  • 1

1 Answers1

0

how about to use Criteria, but i don't have any sql server so i haven't tested yet.

def results = Space.createCriteria().list {
    dynForm{
        like('Test1', 'abc')
    }
}
XenoN
  • 984
  • 1
  • 10
  • 23
  • and u can use `eq('Test1','abc')` too – XenoN May 15 '12 at 10:10
  • Thanks. I am able to filter with the where statement but it is good to know criteria work as well. My real question, though, is how do I get an individual value from the map as a single value in a select. We dynamically assemble the columns in some of our queries and then run them via hql, so I'm looking for the equivalent of: `SELECT elements(space.dynForm['Test1']) . . . ` which does not work. – Rodney Seifert May 15 '12 at 12:29