Question

How can I paginate a query using NameParameterJdbcTemplate?

I was trying with essential jdbcTemplate but I make some mistakes.. this my query:

sql.append("SELECT VGPT_EXE.* FROM ")
     .append(Constants.T_VW_GPT_E_BASIC)
     .append(" AS VGPT_EXE ")
     .append("WHERE VGPT_EXE.")
     .append(Constants.ID_SUBJECTE
    + " in (:listOfValues)");



    PreparedStatementCreatorFactory pscf;
    MapSqlParameterSource parameterss = new MapSqlParameterSource();
    parameterss.addValue("listOfValues", ids);
    pscf = new PreparedStatementCreatorFactory(sql.toString());
    pscf.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);

so the problem I believe I can't setter list values in a namedParameter query? or I'm doing sth wrong.

resultat.setResultats((List<IDeute>) template.getJdbcOperations()
                                .query((PreparedStatementCreator) pscf.newPreparedStatementSetter(new ArrayList<Long>(parameterss.getValues().values())),
                                new ResultSetPaginatExtractor(new MapperDeute(), 
                                resultat.getFiles(), 
                                resultat.getInici())));

ResultSetPaginatExtractor is an implementation of ResultSetExtractor where i can set the number of files and the fisrt row.

the stackTrace:

nested exception is org.springframework.dao.DataRetrievalFailureException: S'ha produit un error en la recuperació del deute associat a la clau de subjecte 3375876; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: SQL [SELECT VGPT_EXE.* FROM vw_gpt_e_basic AS VGPT_EXE WHERE VGPT_EXE.id_subjecte in (:listOfValues) ORDER BY VGPT_EXE.cc_cod_prov, VGPT_EXE.cc_cod_emisor, VGPT_EXE.cc_cod_concepte, VGPT_EXE.cc_any, VGPT_EXE.cc_periode, VGPT_EXE.cc_num_rebut]: given 1 parameters but expected 0
Was it helpful?

Solution

well seems to be an error in NamedParameterdJd.. so I change to jdbcTemplate original.

this is the final code working:

sql.append("SELECT VGPT_EXE.* FROM ")
      .append(Constants.T_VW_GPT_E_BASIC)
      .append(" AS VGPT_EXE ")
      .append("WHERE VGPT_EXE.")
      .append(Constants.ID_SUBJECTE
      + " IN (");

for (int i = 0; i < ids.size(); i++) {
     if (i > 0) {
    sql.append(",");
        }
     sql.append("?");**for every dinamic id add an ?**
     parametres.add(ids.get(i));
     tipusParametres.add(new SqlParameter(Types.INTEGER));
}
    sql.append(")");
    sql.append(" AND VGPT_EXE." + Constants.ID_ENS_EXP + " = ? ");
                parametres.add(idEns);
                tipusParametres.add(new SqlParameter(Types.INTEGER));

                if (tipusDeute != null) {
                    if (tipusDeute.equals(ETipusDeute.HISTORIC)) {
                        sql.append(" AND " + Constants.DATA_FI_VOL
                                + " < TODAY ");
                    }
                    if (tipusDeute.equals(ETipusDeute.VIGENT)) {
                        sql.append(" AND " + Constants.DATA_FI_VOL
                                + " >= TODAY ");
                    }
                }
                // ordenació per la clau
                sql.append(ORDER_BY_E);
                try {

                pscf = new PreparedStatementCreatorFactory(sql.toString());
                // El resultset HA DE SER DEL TIPUS TYPE_SCROLL_INSENSITIVE
                pscf.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
                for (SqlParameter sp : tipusParametres) {
                    pscf.addParameter(sp);
                }

                resultat.setResultats((List<IDeute>) template.getJdbcOperations().query(pscf.newPreparedStatementCreator(parametres.toArray()), new ResultSetPaginatExtractor(new MapperDeute(), 
                        resultat.getFiles(), 
                        resultat.getInici())));

I hope it helps sone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top