如何获取NativeQuery的result参数

时间:2012-08-02 12:48:45

标签: postgresql gwt postgis

我想进行此查询:

Select
    srid,
    substring(srtext
              from (position('DATUM["' in srtext)+7)
              for (position('ID["' in srtext)+2)
                - (position('DATUM["' in srtext)+7))
  from spatial_ref_sys
  order by substring

所以,我创建了一个

Query query = em().createNativeQuery(QUERY)

还创建一个具有Long srid和String sridText的Srid对象。

我需要获取这些值并将其放入List中。 sridText上的子串,显然是srid进入Long srid。

请帮忙!!!

1 个答案:

答案 0 :(得分:1)

找到答案。首先,我做了一个名为getSrid的plpgsql函数

CREATE OR REPLACE FUNCTION getSRID() returns text[] as $$
declare
 resultado text[];
 consulta cursor for
 Select srid,substring(srtext from (position('DATUM["' in srtext)+7) 
 for(position('ID["' in srtext)+2)
  - (position('DATUM["' in srtext)+7)) as sub
 from spatial_ref_sys;
 cont int;
 i int;
 sridd int;
 srTextt text;
begin
 open consulta;
 i=1;
 EXECUTE 'SELECT COUNT(*) FROM SPATIAL_REF_SYS' into cont;
 resultado := ARRAY[cont];
 while(i<cont) loop
  fetch consulta into sridd,srTextt;
  resultado[i] := sridd;
  resultado[i+1] := srTextt;
  i:=i+2;
 end loop;
 return resultado;
end
$$ language plpgsql; 

如你所见,我在返回时得到了一个Text [],所以在我的java调用中我将对象转换为String []:

public List<SridDTO> listaa(){
    int i = 0;
    List<SridDTO> list = new ArrayList<SridDTO>();
    Query query = em().createNativeQuery("Select getSrid()");
    String[] vector = (String[]) query.getSingleResult();
    while(i<vector.length){
        SridDTO sridDTO = new SridDTO(new Long(vector[i]),vector[i+1]);
        i = i + 2;
        list.add(sridDTO);
    }
    return list;
}