Search code examples
grailsarraylisthashmaphqlgrails-orm

GORM - fetch results in List of Map form using HQL


I have a domain name TestCase. I'm fetching the data using HQL as below :-

def query = """
          select 
          tc.testCaseObjective as tco,
          tc.testCaseStatus as tcs
          from TestCase tc

        """

println TestCase.executeQuery(query, [max: 2])

It gives me output as :-

[["Test Case 01", "Pass"], ["work order", "Pass"]]

which is in List of List form.

But actually I want List of Map form as :-

[[tco:"Test Case 01", tcs:"Pass"], [tco:"work order", tcs:"Pass"]]

Can anyone suggest me how to achieve this? I don't want to convert it explicitly.


Solution

  • Got the solution using the select new map syntax in HQL to fetch the results in List of Map as below :-

    def query = """
              select 
              new map(tc.testCaseObjective as tco,
              tc.testCaseSummary as tcs)
              from TestCase tc
    
            """
    
    println TestCase.executeQuery(query, [max: 2])
    

    Output :-

    [[tco:"Test Case 01", tcs:"Pass"], [tco:"work order", tcs:"Pass"]]