0

I have a query that looks like this...

        def data = session.createSQLQuery("""SELECT
  a.id AS media_id,
  a.uuid,
  a.date_created,
  a.last_updated,
  a.device_date_time,
  a.ex_time,
  a.ex_source,
  a.sequence,
  a.time_zone,
  a.time_zone_offset,
  a.media_type,
  a.size_in_bytes,
  a.orientation,
  a.width,
  a.height,
  a.duration,
  b.id           AS app_user_record_id,
  b.app_user_identifier,
  b.application_id,
  b.date_created AS app_user_record_date_created,
  b.last_updated AS app_user_record_last_updated,
  b.instance_id,
  b.user_uuid
FROM media a, app_user_record b
WHERE a.uuid = b.user_uuid
LIMIT :firstResult, :maxResults """)
                .setInteger("firstResult", cmd.firstResult)
                .setInteger("maxResults", cmd.maxResults)
                .list()

The problem is the .list method returns an array that has no column names. Does anybody know of a way to include/add the column names from a Grails native sql query. I could obviously transform the results into a map and hard code the column names myself.

4

1 回答 1

4

用于setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)查询。这将产生条目映射。

import org.hibernate.Criteria

def query = """Your query"""
def data = session.createSQLQuery(query)
                .setInteger("firstResult", cmd.firstResult)
                .setInteger("maxResults", cmd.maxResults)
                .setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)
                .list()

data.each{println it.UUID}

我对其进行了测试,并意识到之前我曾经使用列号而不是列名来获取每个字段。

注意
键是大写的。在结果图中也是ex_source如此。EX_SOURCE

于 2013-09-17T00:17:49.137 回答