2012-08-02 113 views
1

我想使這個查詢:如何獲得NativeQuery的結果參數

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) 

也營造出具有悠久的SRID和一個SRID對象字符串sridText。

我需要得到這些值並放入列表中。 sridText上的子字符串,顯然是進入Long srid的srid。

請幫忙!!!

回答

1

找到了答案。首先,我沒有叫getSrid

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; 

正如你看到的我有一個文本上的[]返回所以在我的Java調用我投的對象爲String []一個PLPGSQL功能:

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; 
}