I passed a lot of time to find the solution but I can't still.
I found some solution about how to pass "IN" elements into PreparedStatement, but if there is any other solution I'd be glad to see that, but, anyway I have
RowMapper<Map<BigInteger, Status>> mapper = new QueryDescriptionById.RowMapperDescription();
@Override
public Multimap<BigInteger, Status> findObject(BigInteger[] ids) {
StringBuilder builder = new StringBuilder();
for (Object id : ids) {
builder.append("?,");
}
String statement = SQL + builder.deleteCharAt(builder.length() - 1).toString() + ")";
Multimap<BigInteger, Status> multimap = ArrayListMultimap.create();
intoMap(multimap, getJdbcTemplate().query(statement, ps -> {
int index = 1;
for (BigInteger id : ids) {
ps.setInt(index++, id.intValue());
}
}, mapper));
return multimap;
}
private void intoMap(Multimap<BigInteger, Status> multimap, List<Map<BigInteger, Status>> list) {
list.forEach(map -> map.forEach((multimap::put)));
}
class RowMapperDescription implements RowMapper {
@Override
public Map<BigInteger, Status> mapRow(ResultSet rs, int rowNum) throws SQLException {
Status status = new Status();
Map<BigInteger, Status> map = new HashMap<>();
BigInteger id = rs.getBigDecimal("ID").toBigInteger();
BigInteger parentId = rs.getBigDecimal("PARENT_ID").toBigInteger();
status.setId(id);
status.setDescription(rs.getString("attempt_status_text"));
map.put(parentId, status);
return map;
}
}
But it works well while I pass in BigInteger[] only 1 element but when there are 2 or more it doesn't into my mapper.
Why ?
P.S
Instead RowMapper I've tried to use ResultSet like
return getJdbcTemplate().query(SQL, ids, resultSet -> {
Multimap<BigInteger, Status> multimap = ArrayListMultimap.create();
int rowNum = 0;
while (resultSet.next()) {
System.out.println("rownum = " + rowNum);
mapper.mapRow(resultSet, rowNum++).forEach(multimap::put);
}
return multimap;
})
But in the console wasn't any lines which mean that resultSet hasn't next()
P.S.S
My SQL
is where hard so I put only a short version. It looks like
private String SQL = "select el1,el2 from mytable where el3 in(";
I've tried instead in(";
and in :ID
and in = :ID
and in (:ID)
and in (?)
and some other variants which I don't remember (-:
EDITED
Actually, I don't understand, why that works
public QueryDescriptionBySmsId(DataSource dataSource) {
super(dataSource, SQL);
logger.debug("Created QueryStatus");
declareParameter(new SqlParameter("ID", Types.NUMERIC));
compile();
logger.debug("Created QueryStatus");
}
@Override
public Multimap<BigInteger, Status> query(Set<BigInteger> ids) {
Map<String, Set> paramMap = Collections.singletonMap("ID", smsIds);
List l = executeByNamedParam(paramMap); // it returns a lot of elements which I am looking for
}
But that doesn't work
@Override
public Multimap<BigInteger, SmsStatus> query(Set<BigInteger> sids) {
Map<String, Set> paramMap = Collections.singletonMap("ID", ids);
return getJdbcTemplate().query(SQL, resultSet -> {
int rowNum = 0;
Multimap<BigInteger, SmsStatus> multimap =
ArrayListMultimap.create();
while (resultSet.next()) {
mapper.mapRow(resultSet, rowNum++).forEach(multimap::put);
}
return multimap;
},paramMap);
}
It shows an error
SQL state [99999]; error code [17004]; Invalid column type