2013-12-12 41 views
0

存儲功能,我有以下類型的PostgreSQL:春天 - 調用複雜的結果

CREATE TYPE TR_PERSON AS (
i_out integer, 
str_out text 
); 

而且我已存儲的函數返回我喜歡的類型:

CREATE OR REPLACE FUNCTION test_function(id int) 
RETURNS TR_PERSON 
AS $$ 
SELECT $1, text('Alice') 
$$ LANGUAGE SQL; 

我試圖使用從SimpleJdbcCall時春天從數據庫獲取數據:

SimpleJdbcCall call = new SimpleJdbcCall(jdbcTemplate) 
     .withFunctionName("test_function"); 

SqlParameterSource in = new MapSqlParameterSource().addValue("id", 1); 

try { 
    TRPerson result = call.executeFunction(TRPerson.class, in); 
} catch (DataAccessException e) { 
    logger.log(Level.SEVERE, "call failed", e); 
} 

然後,我得到異常:

SEVERE: call failed 
org.springframework.dao.InvalidDataAccessApiUsageException: Required input parameter 'i_out' is missing 
at org.springframework.jdbc.core.CallableStatementCreatorFactory$CallableStatementCreatorImpl.createCallableStatement(CallableStatementCreatorFactory.java:209) 
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:1014) 
at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1070) 
at org.springframework.jdbc.core.simple.AbstractJdbcCall.executeCallInternal(AbstractJdbcCall.java:387) 
at org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:350) 
at org.springframework.jdbc.core.simple.SimpleJdbcCall.executeFunction(SimpleJdbcCall.java:154) 

我不明白爲什麼i_out被標記爲輸入類型。我做錯了什麼?

SimpleJdbcCall是否適合我的需要?

存儲函數和複雜類型結果的最佳做法是什麼?

我非常感謝一些骨架代碼來捕捉管道。

回答

2

我發現解決方案,也許它不是理想的,但工作得很好,也應該工作以防從函數返回多個記錄(piplined返回)。這裏是。希望它也能幫助你。

String SQL = "select i_out, str_out from test_function1(:id)"; 
SqlParameterSource namedParameters = new MapSqlParameterSource("id", request.getIntTestVar()); 

List<TRPerson> result = namedTemplate.query(SQL, namedParameters, new RowMapper() { 

    @Override 
    public TRPerson mapRow(ResultSet rs, int i) throws SQLException { 
     TRPerson result = new TRPerson(); 
     result.setIntVar(rs.getInt("i_out")); 
     result.setStrVar(rs.getString("str_out")); 
     return result; 
    } 
});