javaspringspring-jdbcjava-stored-procedures

Resultset of stored procedure contains list of LinkedCaseInsensitiveMap<V>


I have this stored procedure call in Java:

@Autowired
public ScoreDao(DataSource dataSource) {
    setDataSource(dataSource);
    mScoreStoredProcedure = new ScoreStoredProcedure(dataSource);
}

public List<Score> loadAllScore(String pUsername, String pUUID, int pLimit) {
    return mScoreStoredProcedure.execute(pUsername, pUUID, pLimit);
}

private class ScoreStoredProcedure extends StoredProcedure {
    private static final String SPROC_NAME = "loadUserScore";

    public ScoreStoredProcedure(DataSource datasource) {
        super(datasource, SPROC_NAME);
        declareParameter(new SqlReturnResultSet("#result-set-2", mScoreMapper));
        declareParameter(new SqlParameter("vusername", Types.VARCHAR));
        declareParameter(new SqlParameter("vuuid", Types.VARCHAR));
        declareParameter(new SqlParameter("vlimit", Types.INTEGER));
        compile();
    }

    @SuppressWarnings("unchecked")
    public List<Score> execute(String pUsername, String pUUID, int pLimit){ 
        Map<String,Object> lAllScoreResult = super.execute(pUsername, pUUID, pLimit);
        List<Score> lAllScore = ((List<Score>)lAllScoreResult.get("#result-set-2"));
        return lAllScore; 
    }

}

and this mapper class:

public class ScoreMapper implements RowMapper<Score> {

private String suffix = "";

@Autowired
private ScoreCreator scoreCreator;

@Autowired
private QuestionMapper questionMapper;

public ScoreMapper(String pSuffix) {
    suffix = pSuffix;
}

public Score mapRow(ResultSet rs, int rowNum) throws SQLException {
    Score lScore = scoreCreator.createScore(rs.getLong(suffix+"id"),
            rs.getTimestamp(suffix+"stempel"), rs.getString(suffix+"username"),
            rs.getInt(suffix+"points"), rs.getInt(suffix+"level"),
            rs.getString(suffix+"comment"),
            questionMapper.mapRow(rs, rowNum), rs.getString(suffix+"uuid"));
    return lScore;
}
  }

The problem I have is that the result of my StoredProcedure is never cast to List<Score>.

Instead it contains a List of LinkedCaseInsensitiveMap<V>. Each entry covers a value from the database.

The mapper is correctly wired via Spring.

Briefly: I expect as result a List<Score>. I thought I handle this with the methods shown in the code. How can I cast the result directly to my List<Score>?

I followed this tutorial http://www.jtmelton.com/2007/10/30/using-springs-storedprocedure-and-rowmapper-mechanisms/

Could you help me find the problem?


Solution

  • Can you rename the result set parameter to, say, "ScoreResultSet"?

    So

    declareParameter(new SqlReturnResultSet("ScoreResultSet", mScoreMapper));
    

    and

    List<Score> lAllScore = ((List<Score>)lAllScoreResult.get("ScoreResultSet"));
    

    The name you are currently using also gets used by spring when it auto generates names for result sets.

    I think you may therefore now be returning a different, unprocessed, result set than the mapped one from the SqlReturnResultSet parameter you declared.

    ======

    EDIT:

    I just found this other question of yours: Java StoredProcedure with SqlReturnResultSet not working and assuming that we are talking about the same stored procedure:

    I suspect you have two selects in the stored procedure and it returns two result sets. You should declare two (properly named) SqlReturnResultSet parameters instead of one. The order of the declaration matters, so you cannot declare just one and give it the name usually assigned to the second one.